We help IT Professionals succeed at work.

Permutation or ascending letter generator

matija_
matija_ asked
on
I'm looking for classic ASP script which would generate 3 letter combination in ascending order, like:

aaa
aab
aac
...
aba
abb
abc
...
aaA
aaB
aaC
...
and so on until Z

I would start first with "aaa" and write it to database, next time, I'll query the database, find "aaa" and continue from there (next generated characters should be "aab")

Can anyone help? Thanks.
Comment
Watch Question

Programmer-analyst
Commented:
I assumed always you have a string of just 3 characters (no numbers).

Here is your code:

Function NextValue(s)
Dim o, v
o = ""
If Len(s)<3 Then
	o = String(3-Len(s),"a")
End If
v = BaseEncode(BaseDecode(s) + 1)
If Len(v)<3 Then
	v = String(3-Len(v),"a") & v
End If
o = o & v
NextValue = o
End Function

Function BaseEncode(number)
Dim alphabet
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
If number = 0 Then
    BaseEncode = "a"
    Exit Function
End If
BaseEncode = vbNullString
Do While number <> 0
    BaseEncode = Mid(alphabet, number Mod 52 + 1, 1) & BaseEncode
    number = number \ 52
Loop
End Function
 
Function BaseDecode(base)
Dim alphabet
alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
BaseDecode = InStr(alphabet, Right(base, 1)) - 1
For i = Len(base) - 1 To 1 Step -1
    BaseDecode = BaseDecode + 52 ^ (Len(base) - i) * (InStr(alphabet, Mid(base, i, 1)) - 1)
Next
End Function

' Here is some test, modify start variable to test more
start = "aba"
Response.Write "Next from " & start & " is " & NextValue(start) & "<br>" & vbcrlf

Open in new window


Hope it helps.

Author

Commented:
Perfect, thanks.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.