Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Alphabetic Counter 1

Status
Not open for further replies.

AndyInNC

Programmer
Sep 22, 2008
76
US
I'm trying to write a counter that uses the alphabet as the count like this:
[ul]
[li]A[/li]
[li]B[/li]
...
[li]Y[/li]
[li]Z[/li]
[li]AA[/li]
[li]AB[/li]
...
[li]AZ[/li]
[li]BA[/li]
[li]BB[/li]
....
[li]ZY[/li]
[li]ZZ[/li]
[li]AAA[/li]
[li]AAB[/li]
[/ul]


Here's my current (incomplete) code. It's a recursive function where you pass in a counter value and it's converted to a letter via Chr(). It's close, but at the transition from AY, AZ, BA, BB it begins to have problems and miscalculates either AZ or BA depending on adjusting the increment.

Take a look & if you've already got one written, that'll be great!

Code:
[blue]Dim[/blue] x
[blue]For[/blue] x = 1 [blue]to[/blue] 100
    [green]'debug.print sAlphaCounter(x)[/green]
    response.write(sAlphaCounter(x) & "</br>")
[blue]Next[/blue]

[blue]Function[/blue] sAlphaCounter([blue]ByVal[/blue] iCountValue)
    [blue]Dim[/blue] sRetVal
    sRetVal = ""
    [blue]If[/blue] iCountValue > 26 [blue]Then[/blue]
        sRetVal = sAlphaCounter(Int(iCountValue/26))
        [blue]If[/blue] 26 [blue]Mod[/blue] iCountValue = 0 [blue]Then[/blue]
            iCountValue = iCountValue + 1
        [blue]Else[/blue]
            iCountValue = iCountValue - (Int(iCountValue/26) * 26)
        [blue]End If
    End If[/blue]
    iCountValue = iCountValue + 64
    sRetVal = sRetVal & Chr(iCountValue)
    sAlphaCounter = sRetVal
[blue]End Function[/blue]
 
You error was a logic one in that when you hit the 2nd and beyond loops, it was jumping ahead early e.g. x = 52 would start the B's of the last of the A's

This is the code I used to get what you wanted. There are only a few minor changes. I've tested to x to the value of 200 and it works thus far.

P.S. Logic problems are a pain in the butt lol

Code:
dim x
for x = 1 to 200
   strOutput=strOutput & sAlphaCounter(x) & vbtab
Next
wscript.echo strOutput

Function sAlphaCounter(ByVal iCountValue)
    Dim sRetVal
    sRetVal = ""
    If iCountValue > 26 Then
        sRetVal = sAlphaCounter(int((iCountValue-1)/26))
        If iCountValue mod 26 = 0 Then
            iCountValue = 26
        Else
            iCountValue = iCountValue - (Int(iCountValue/26) * 26)
        End If
    End If
    iCountValue = iCountValue + 64
    sRetVal = sRetVal & Chr(iCountValue)
    sAlphaCounter = sRetVal
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top