BigDaddyO Posted December 18, 2019 Posted December 18, 2019 (edited) I'm trying to build a generator so I can do a while loop and cycle through alphanumeric values until I find one that hasn't been used in our database yet. I started with the Excel Number to Column letter converter function and it sorta works but I can't get zero's to show up properly. $i = 0 displays nothing. $i = 36 = 0, shouldn't it be 00, I'm not handling the zeros properly and i'm not sure where i'm going wrong. Any ideas? $i = 36 MsgBox(0, $i, _AlphaNumCode($i)) Func _AlphaNumCode($int) If Not StringRegExp($int, "^[0-9]+$") Then Return SetError(1, 0, "") Local $sCode, $iTemp While $int $iTemp = Mod($int, 36) ConsoleWrite("Mod(" & $int & ", 36) = " & $iTemp & @CRLF) If $iTemp <= 0 Then $iTemp = 36 $sCode = 0 & $sCode ElseIf $iTemp < 10 Then $sCode = $iTemp & $sCode Else $sCode = Chr(($iTemp - 9) + 64) & $sCode EndIf $int = ($int - $iTemp) / 36 WEnd Return $sCode EndFunc Thanks Edited December 19, 2019 by BigDaddyO fix
jitb Posted December 18, 2019 Posted December 18, 2019 ; - I don't see the problem = (0 - 0) /36 is still zero $i = 36 MsgBox(0, $i, _AlphaNumCode($i)) Func _AlphaNumCode($int) If Not StringRegExp($int, "^[0-9]+$") Then Return SetError(1, 0, "") Local $sCode, $iTemp While $int $iTemp = Mod($int, 36) ; - =0 ConsoleWrite("Mod(" & $int & ", 36) = " & $iTemp & @CRLF) If $iTemp <= 0 Then ; = 0 $iTemp = 36 $sCode = 0 & $sCode ; - 0 & 0 ElseIf $iTemp < 10 Then $sCode = $iTemp & $sCode Else $sCode = Chr(($iTemp - 9) + 64) & $sCode EndIf $int = ($int - $iTemp) / 36 ; = (0 - 0) /36 is still zero WEnd $sCode = StringFormat("%02s", $sCode); Return $sCode EndFunc
jitb Posted December 18, 2019 Posted December 18, 2019 #include <excel.au3> MsgBox(0, 36, _Test01()) Func _Test01() $i_Counter = 1 While 1 $s_Dummy01 = _Excel_ColumnToLetter($i_Counter) If $s_Dummy01 = 'AJ' Then ExitLoop $i_Counter += 1 WEnd Return $i_Counter & '|' & $s_Dummy01 EndFunc ;==>_Test01
BigDaddyO Posted December 19, 2019 Author Posted December 19, 2019 (edited) If I pass 0, then it should return 0 if I pass 10, it should return A if I pass 35, it should return Z if I pass 36, it should return 00 if I pass 71, it should return 0Z 72 should be 10 it currently sees 0 as being greater than Z when it should be the first one Edited December 19, 2019 by BigDaddyO
mikell Posted December 19, 2019 Posted December 19, 2019 This method looks inconsistent because the result should always be 2 chars long Using such a way $int = 73 $s = "0123456789abcdefghijklmnopqrstuvwxyz" $a = StringSplit($s, "") $n = Int($int/$a[0]) $m = Mod($int, $a[0]) msgbox(0,"", $a[$n+1]) & $a[$m+1]) Then you get this : If I pass 0, then it should return 00 if I pass 10, it should return 0A if I pass 35, it should return 0Z if I pass 36, it should return 10 if I pass 71, it should return 1Z 72 should be 20 etc Much more cute and efficient IMHO
jchd Posted December 19, 2019 Posted December 19, 2019 That's way better as this restores the null value of 0. Making the sequence be 0 1.. A .. Z 00 01 .. 0Z .. hides the fact that there is an "empty digit" in the first 36 values. Position counting has proven to be solid, so better use it! This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
BigDaddyO Posted December 19, 2019 Author Posted December 19, 2019 I was hoping to use this for a few different instances. One of my fields contains 2 character AlphaNumeric Another contains 4 characters so I was just going to start the loop # at a different point depending on how many characters I need to generate.
BigDaddyO Posted December 26, 2019 Author Posted December 26, 2019 Using @mikell idea of setting the values into an array instead of using Chr simplified it enough that I was able to wrap my mind around this a bit better. This seems to be working for me and I've got some examples on where to start depending on how many characters you want returned expandcollapse popup;----------------------------------------------------------------------------------------------------- ; Name........... _AlphaNumCode() ; Description.... Generates an AlphaNumeric code of any length depending on the Int you pass into it ; Syntax......... _AlphaNumCode($int) ; Parameters .... $int = The integer that you want to convert into an AlphaNumeric value ; Return values.. Success = The AlphaNumeric value 0 through Z of any length ; Failure = Sets @error ; Author ........ BigDaddyO ; Modified by.... ; Remarks ....... ; Example........ ; $i = 72 ; = 20 ; $i = 1296 ; = 100 ; $i = 2592 ; = 200 ; $i = 2592 ; = 300 ; $i = 46656 ; = 1000 ;Start of 4 digit AlphaNumeric ; $i = 1679616 ; = 10000 ;Start of 5 digit AlphaNumeric ; $i = 60466176 ; = 100000 ;Start of 6 digit AlphaNumeric ; $i = 2176782336 ; = 1000000 ;Start of 7 digit AlphaNumeric ; For additional characters, Multiply the start of the previous # of characters by 36 to get the start of the next number of digits ; 2176782336 * 36 = 78364164096 ;This is the start of 8 digit AlphaNumeric ;----------------------------------------------------------------------------------------------------- Func _AlphaNumCode($int) Local $aChr[36] = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"] If Not StringRegExp($int, "^[0-9]+$") Then Return SetError(1, 0, "") Local $sCode, $iMod If $int = 0 Then Return "0" While $int $iMod = Mod($int, 36) ConsoleWrite("Mod(" & $int & ", 36) = " & $iMod & @CRLF) $sCode = $aChr[$iMod] & $sCode $int = ($int - $iMod) / 36 WEnd Return $sCode EndFunc
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now