10-20-2011 02:59 PM
I'm writing a program that generates a large number of report files. Many of them might have the same initial filename, so I need to append a character (A, B, C, D, etc) to the end of the filename so that each name is unique.
It's easy enough to use a while loop and check if a filename already exists, increment the suffix as needed and arrive at the first unused name. To comply with our current systems, the suffix has to be an upper case letter. (Using an integer would make this much easier.)
So what I need is a function that maps integers to uppercase letters such as: 0->A, 1->B, 2->C...26->AA, 27->AB, 28->AC, etc.
The attached code works, but it really seems to me there would be an easier way of doing this.
Any ideas?
10-20-2011 03:54 PM - edited 10-20-2011 03:58 PM
Action Engine to the rescue
Harder than it looks.
I usually prepend the reports name with Date and Time as YYJJJHHMMSS
year in centurary, day in year (julian date), hour 0-24, minute, second. This also has the happy side effect of ordering the reports in the file
10-20-2011 04:35 PM
I have no problem using Mod arithmetic in this scenario, but if I knew for example that there would be less than 702 duplicates of any given file then I would probably do something like this:
You could easily add a third letter to the lookup table without too much of a memory hit.
10-20-2011 04:43 PM
I started out with Array Index, but did have a few instances where I had to go to three or maybe four characters, so I wanted something that wasn't limited.
I was kinda hoping there was a fancy way of using regex or format into string or something that uses the logic of ordered characters, rather than the brute force of counting.
Oh well.
10-20-2011 05:28 PM
Just for fun here is another way to do it.
Since recursion does not work in snippets I have attached it as well.
For some numerology I was testing its speed on the classic text speak LMAO (219740), I was a bit surprised at what happens when you decrement the first digit (subtract 100000).
10-20-2011 05:39 PM
241310672!