Generating a code range (e.g. aaaa / aadv)

xistence

Newcomer
Joined
Dec 1, 2003
Messages
2
Hi there,

I'm having a little problem which I can't figure out, I'm really not having my day I think. I'm hoping someone could help me with this and give me the solution on the following problem:

I need a function which will generate a collection of "codes". These codes will exist out of four characters between "a" and "z", for instance: from "aaaa" till "aadv" (a range of 100 codes). The function should take two parameters, the first would be the code to start from (for instance "aaaa") and the second parameter should be the ammount of codes that should be generated (for instance 100).

I would really appriciate it if someone could help me with this ploblam, I'm on this for the whole day now and I'm really getting nowhere at all.

Thanks in advance,

Maurits
 
Each letter has an ascii code. Not sure what A is but lets say its 98. Your string to represent aaaa would be 98 98 98 98. Find out what A is, just for this lets say 100 to make things simple. Store your letters in an array.

Then write some code to go through and increase each letter.

For i = 0 to 99 ' or whatever you need it to be

If code(3) = 100 then
code(2) += 1 'Goes to the next letter
code(3) = code(98) ' Back to A
Else
code(3) += 1
End If

Next

You will need more IF statements to satisy each position in the code.

Hope this makes sense or gives you an idea on how it could be done.
 
First off all many thanks sjn78, for the solution you gave me it works perfect.

However I wasn't quite happy with the ammount of IF statements I needed to accomplish the task, so I made some modifications to the code you gave me. Another advantage of the modifications I made is, I can use it with codes with different ammount of characters. I will place code here, so you can check it out and maybe use it yourself, or maybe you got some points to improve it. But again many thanks, if it wasn't for you I still was eating up my nails and throwing stuff at my monitor.

Visual Basic:
Private Function GenerateRange(ByVal strRangeFrom As String, ByVal intAmmount As Integer) As Array
      '--- Declare local variables
      Dim i, intArrayCount As Integer
      Dim strLetters() As String
      Dim strReturn As String
      Dim lngUsed As Long : lngUsed = 0
      Dim lngFree As Long : lngFree = 0
      Dim arrKeyword() As String

      '--- Prepare array's
      ReDim arrKeyword(intAmmount - 1)
      ReDim strLetters(Len(Trim(strRangeFrom)))

      '--- Split string into array
      For i = 0 To UBound(strLetters) - 1
         strLetters(i) = Mid(LCase(strRangeFrom), i + 1, 1)

         If i = (UBound(strLetters) - 1) Then
            lngUsed = lngUsed + (Asc(strLetters(i)) - 97)
         Else
            lngUsed = lngUsed + (((Asc(strLetters(i)) - 97)) * (26 ^ (UBound(strLetters) - (i + 1))))
         End If
      Next

      '--- Check how much codes are free to use
      lngFree = (26 ^ UBound(strLetters)) - lngUsed

      '--- Determine the ammount of characters are used for the 
      '--- code
      intArrayCount = UBound(strLetters) - 1

      If lngFree >= intAmmount Then
         '--- Insert first keyword into return array
         arrKeyword(0) = strRangeFrom

         '--- Generate other codes
         For i = 1 To intAmmount - 1
            '--- Iterate through all letters in the string
            '--- until we found one wich doesn't equals "z"
            Do While strLetters(intArrayCount) = "z"
               intArrayCount = intArrayCount - 1
            Loop

            '--- Increase the letter by one   
            strLetters(intArrayCount) = Chr(Asc(strLetters(intArrayCount)) + 1)


            '--- Set all letters after the letter we just increased
            '--- to "a"   
            Do While intArrayCount < UBound(strLetters) - 1
               intArrayCount = intArrayCount + 1
               strLetters(intArrayCount) = "a"
            Loop

            arrKeyword(i) = Join(strLetters, "")
         Next
      End If

      Return arrKeyword
   End Function
 
Glad to help out. I knew there was going to be a lot of IF's, and there was surely a more efficient way to do it, but that was what first popped to mind.
 
Back
Top