LIMITER Posted February 9, 2008 Posted February 9, 2008 How can i split a randomly generated number after each 4 characters ?Like a serial key (XXXX-XXXX-XXXX-XXXX-XXXX) ?THX !!!!
rasim Posted February 9, 2008 Posted February 9, 2008 Hi! Try this: Dim $string = "123456789012", $result $aNumber = StringRegExp($string, "\d{4}", 3) For $i = 0 To UBound($aNumber) - 1 $result &= $aNumber[$i] & "-" Next $result = StringTrimRight($result, 1) MsgBox(0, "", $result)
BigDod Posted February 9, 2008 Posted February 9, 2008 (edited) Here is my feeble attempt #include<string.au3> #include <File.au3> _FileCreate("c:\test.txt") $in = "12341234123412341234" For $i = 4 To 19 Step 5 $in = _StringInsert($in, "-", $i) Next _FileWriteToLine("c:\test.txt", 1, $in, 1) Edited February 9, 2008 by BigDod Time you enjoyed wasting is not wasted time ......T.S. Elliot Suspense is worse than disappointment................Robert Burns God help the man who won't help himself, because no-one else will...........My Grandmother
Emiel Wieldraaijer Posted February 9, 2008 Posted February 9, 2008 (edited) if you are using the format XXXX-XXXX-XXXX-XXXX-XXXX $String = Stringsplit("XXXX-XXXX-XXXX-XXXX-XXXX", "-") For $i = 1 to $String[0] Msgbox (0, "Split", $String[$i]) Next Edited February 9, 2008 by Emiel Wieldraaijer Best regards,Emiel Wieldraaijer
PsaltyDS Posted February 9, 2008 Posted February 9, 2008 Yet another, etc... assemble while generating the random:$sTxt = "" For $n = 1 To 5 $sTxt &= Random(0, 9999, 1) If $n < 5 Then $sTxt &= "-" Next MsgBox(64, "Result", "$sTxt = " & $sTxt)oÝ÷ Øw±½êìÚºÚ"µÍÌÍÜÕH ][ÝÉ][ÝÂÜ ÌÍÛHHÈ BIÌÍÜÕ [ÏH^ [ÛJ MHHKJK BRY ÌÍÛ È H[ ÌÍÜÕ [ÏH ][ÝËI][ÝÂ^ÙÐÞ ][ÝÔÝ[ ][ÝË ][ÝÉÌÍÜÕH ][ÝÈ [È ÌÍÜÕ oÝ÷ Ø i©îêâr÷«²*'jëh×6$avChars = StringSplit("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", "") $sTxt = "" For $i = 1 To 5 For $n = 1 To 4 $sTxt &= $avChars[Random(1, 36, 1)] Next If $i < 5 Then $sTxt &= "-" Next MsgBox(64, "Result", "$sTxt = " & $sTxt) Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Crowbar Posted March 23, 2008 Posted March 23, 2008 Ok, I know this is an old(er) topic, so I apologize... BUT- This is all great unless you want to add the "-" in realtime. For example, I am using a GUICtrlCreateInput() and am trying to automatically add the "-" after every x character (in realtime) For example: The user types 1234, the input boxes throws "-", the user types 5678, Input throws "-", etc, up to the max (which, including the hyphens, is 25 characters). Here's what I've tried: CODE$VirusKeyIn = GUICtrlRead($VirusKey) For $i = 4 To 19 Step 5 $VirusKeyIn = _StringInsert($VirusKeyIn, "-", $i) GUICtrlSetData($VirusKey, $VirusKeyIn) Next Also tried: CODE$VirusKeyIn = GUICtrlRead($VirusKey) For $i = 4 To 19 Step 5 If StringLen($VirusKeyIn) > $i Then $VirusKeyIn = _StringInsert($VirusKeyIn, "-", $i) GUICtrlSetData($VirusKey, $VirusKeyIn) Next Here's the interesting thing: When refreshed (this Input works for two different keys, so when I switch it to the other key using the ComboBox and then switch back), THEN it throws in the key! Is what I'm attempting to do even possible with AutoIt? Thank you so much!
PsaltyDS Posted March 23, 2008 Posted March 23, 2008 You're in luck. My cookie dough is chilling in the refrigerator before I start baking, and I'm bored:expandcollapse popup#include <GuiConstants.au3> Opt("GuiOnEventMode", 1) Global $hGUI, $Input1, $sText = "" $hGUI = GuiCreate("Test", 300, 200) GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit") GUICtrlCreateLabel("Enter your key as xxxx-xxxx-xxxx-xxxx", 10, 10, 280, 20) $Input1 = GUICtrlCreateInput("", 10, 40, 280, 20) GUICtrlCreateButton("OK", 100, 140, 100, 30) GUICtrlSetOnEvent(-1, "_OK") AdlibEnable("_FormatInput", 250) GUISetState() While 1 Sleep(20) WEnd Func _FormatInput() Local $sRead = ControlGetText($hGUI, "", $Input1) If $sRead <> $sText Then $sRead = StringStripWS(StringReplace($sRead, "-", ""), 8) Local $sWrite = "" For $n = 1 To StringLen($sRead) Step 4 $sWrite &= StringMid($sRead, $n, 4) & "-" Next $sWrite = StringTrimRight($sWrite, 1) $sText = $sWrite ControlSetText($hGUI, "", $Input1, $sWrite) ControlSend($hGUI, "", $Input1, "{END}") If StringLen($sWrite) = 19 Then _OK() EndIf EndFunc Func _OK() ; Get input Local $sRead = ControlGetText($hGUI, "", $Input1) Local $avRead = StringSplit($sRead, "-") ; Test for valid format Local $fValid = True If $avRead[0] = 4 Then For $n = 1 To 4 If StringLen($avRead[$n]) <> 4 Then $fValid = False ExitLoop EndIf Next Else $fValid = False EndIf ; Show results If $fValid Then MsgBox(64, "Congratulations!", "You entered a valid key format: " & $sRead) Exit Else MsgBox(16, "Error!", "Your key format is invalid: " & $sRead & @CRLF & "No soup for you!") ControlSetText($hGUI, "", $Input1, "") EndIf EndFunc Func _Quit() Exit EndFunc Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Paulie Posted March 23, 2008 Posted March 23, 2008 If you want to stringsplit by a certain number of characters, use _StringChop #include <Array.au3> _ArrayDisplay(_StringChop("1234-1234-1234-1234",5)) Func _StringChop($string, $size) $count = Ceiling(StringLen($string)/$size) Dim $array[$count+1], $start = 1 For $i = 1 To $count $array[$i] = StringMid($string, $start, $size) $start += $size Next $array[0] = $count Return $array EndFunc
Moderators SmOke_N Posted March 23, 2008 Moderators Posted March 23, 2008 If you want to stringsplit by a certain number of characters, use _StringChop #include <Array.au3> _ArrayDisplay(_StringChop("1234-1234-1234-1234",5)) Func _StringChop($string, $size) $count = Ceiling(StringLen($string)/$size) Dim $array[$count+1], $start = 1 For $i = 1 To $count $array[$i] = StringMid($string, $start, $size) $start += $size Next $array[0] = $count Return $array EndFuncOr _SomeOtherStringChop Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
Paulie Posted March 23, 2008 Posted March 23, 2008 Or_SomeOtherStringChop Roflcopters! I remember that!
FreeFry Posted March 23, 2008 Posted March 23, 2008 Ok, I know this is an old(er) topic, so I apologize... BUT- This is all great unless you want to add the "-" in realtime. For example, I am using a GUICtrlCreateInput() and am trying to automatically add the "-" after every x character (in realtime)For example: The user types 1234, the input boxes throws "-", the user types 5678, Input throws "-", etc, up to the max (which, including the hyphens, is 25 characters).Here's what I've tried:CODE$VirusKeyIn = GUICtrlRead($VirusKey)For $i = 4 To 19 Step 5$VirusKeyIn = _StringInsert($VirusKeyIn, "-", $i)GUICtrlSetData($VirusKey, $VirusKeyIn)NextAlso tried:CODE$VirusKeyIn = GUICtrlRead($VirusKey)For $i = 4 To 19 Step 5If StringLen($VirusKeyIn) > $i Then $VirusKeyIn = _StringInsert($VirusKeyIn, "-", $i)GUICtrlSetData($VirusKey, $VirusKeyIn)NextHere's the interesting thing: When refreshed (this Input works for two different keys, so when I switch it to the other key using the ComboBox and then switch back), THEN it throws in the key! Is what I'm attempting to do even possible with AutoIt?Thank you so much!I would have had a temporary buffer, storing up to 4 chars in it, and when it would hit 4, it would add a - to the Edit control, and empty the buffer again.also I would perhaps store the "old" value in the Edit control so I could check whenever it has changed(to avoid having to check it all the time, etc.)
Crowbar Posted March 23, 2008 Posted March 23, 2008 Thank you so much! This had me stumped for days! Also, thanks for the reference to the Soup Nazi, PsaltyDS! Enjoy your cookies!
PsaltyDS Posted March 23, 2008 Posted March 23, 2008 Thank you so much! This had me stumped for days! Also, thanks for the reference to the Soup Nazi, PsaltyDS! Enjoy your cookies!Glad it helped. The cookies are done now, and they came out great! Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Moderators SmOke_N Posted March 23, 2008 Moderators Posted March 23, 2008 Glad it helped. The cookies are done now, and they came out great! Hey Salty... could have made that function 1 line :$sString = "1234123412341234" $sTemp = StringTrimRight(StringRegExpReplace($sString, "\d{4}", "\0-"), 1) MsgBox(0, 0, $sTemp) Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
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