FinalVersion Posted February 21, 2010 Posted February 21, 2010 Lets say I have a string like this "0765898743537567". I want to put every 4 chars into an array, so I'm thinking a For loop, and StringMid, but I'm lost on how to do this. [center][+] Steam GUI [+][+] Clipboard Tool [+][+] System :: Uptime [+][+] StarCraft II Mouse Trap [+][/center]
BrettF Posted February 21, 2010 Posted February 21, 2010 #include <Array.au3> $string = "0000100001000010000100001000010000100001" $aString = _GetEvery ($string, 5) MsgBox (0, "", $aString) _ArrayDisplay ($aString) Func _GetEvery ($string, $iTImes) Local $count = 0 $len = StringLen ($string) Local $aStrings[Int($len/$iTImes)+1] For $i = 1 To $len + 1 Step $iTImes $aStrings[$count] = StringMid ($string, $i-1, 1) $count += 1 Next Return $aStrings EndFunc Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
ResNullius Posted February 21, 2010 Posted February 21, 2010 Or with the power of RegExp: #include <Array.au3> $string = "0000100001000010000100001000010000100001" $aString = _GetEvery ($string, 5) _ArrayDisplay($aString) Func _GetEvery ($string, $iTimes) $aString = StringRegExp($string, ".{" & $iTimes-1 & "}(.)",3) Return $aString EndFunc
BrettF Posted February 21, 2010 Posted February 21, 2010 Or with the power of RegExp: #include <Array.au3> $string = "0000100001000010000100001000010000100001" $aString = _GetEvery ($string, 5) _ArrayDisplay($aString) Func _GetEvery ($string, $iTimes) $aString = StringRegExp($string, ".{" & $iTimes-1 & "}(.)",3) Return $aString EndFunc Hehehehe was trying something like that but I just gave up and went old school... Still learning those damn things 8) Vist my blog!UDFs: Opens The Default Mail Client | _LoginBox | Convert Reg to AU3 | BASS.au3 (BASS.dll) (Includes various BASS Libraries) | MultiLang.au3 (Multi-Language GUIs!)Example Scripts: Computer Info Telnet Server | "Secure" HTTP Server (Based on Manadar's Server)Software: AAMP- Advanced AutoIt Media Player | WorldCam | AYTU - Youtube Uploader Tutorials: Learning to Script with AutoIt V3Projects (Hardware + AutoIt): ArduinoUseful Links: AutoIt 1-2-3 | The AutoIt Downloads Section: | SciTE4AutoIt3 Full Version!
dani Posted February 21, 2010 Posted February 21, 2010 (edited) Actually neither of your solutions does what he wants -- as I understand it. He wants to get every x characters into an array. So if you have the string "12345678" he want to have the result $a[0] = "1234", $a[1] = "5678".Modified the code of a ResNullius a bit to provide this. This ofcourse only works properly when StringLen($string) mod $iTimes = 0. If not, the last characters < $iTimes won't appear in the array.#include <Array.au3> $string = "0000100001000010000100001000010000100001" $aString = _GetEvery ($string, 5) _ArrayDisplay($aString) Func _GetEvery ($string, $iTimes) $aString = StringRegExp($string, "(.{" & $iTimes & "})",3) Return $aString EndFunc Edited February 21, 2010 by d4ni
picaxe Posted February 21, 2010 Posted February 21, 2010 Modified the code of a ResNullius a bit to provide this. This ofcourse only works properly when StringLen($string) mod $iTimes = 0. If not, the last characters < $iTimes won't appear in the array. When last characters are required#include <Array.au3> $string = "0000100001000010000100001000010000100001001" $aString = _GetEvery ($string, 5) _ArrayDisplay($aString) Func _GetEvery ($string, $iTimes) $aString = StringRegExp($string, "(.{" & $iTimes & "}|.{1," & $iTimes & "})",3) Return $aString EndFunc
dani Posted February 21, 2010 Posted February 21, 2010 (edited) No need for the OR operation, you can just do it like this:#include <Array.au3> $string = "0000100001000010000100001000010000100001" $aString = _GetEvery ($string, 5) _ArrayDisplay($aString) Func _GetEvery ($string, $iTimes) $aString = StringRegExp($string, "(.{1," & $iTimes & "})",3) Return $aString EndFuncIt's greedy by default so always tries to match $iTimes characters first, when it fails steps back to $iTimes-1, etc.I just figured he might have wanted it to not include characters that are not a multiple of x Edited February 21, 2010 by d4ni
FinalVersion Posted February 21, 2010 Author Posted February 21, 2010 @d4ni Exactly what I wanted, I just had to change $iTimes to 4. [center][+] Steam GUI [+][+] Clipboard Tool [+][+] System :: Uptime [+][+] StarCraft II Mouse Trap [+][/center]
dani Posted February 21, 2010 Posted February 21, 2010 (edited) @d4niExactly what I wanted, I just had to change $iTimes to 4.Credits go to ResNullius who started the Regular Expression. Picaxe and I only modified it a bit.Remember you don't have to change $iTimes directly, but only the number you use in the function call _GetEvery -- it's 5 there now, so if you just make that 4 it'll work. Edited February 21, 2010 by d4ni
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