AutID Posted January 18, 2014 Share Posted January 18, 2014 Hi, How can i convert my string to an array if each time my string is added like this: Local $string $string = "text1" $string = "text2" $string = "text3" Any other solutions better than this? Local $string[1] Local $1 = 1 For $i = 1 To 3 _ArrayAdd($string, "text" & $1) $1 += 1 Next _ArrayDisplay($string) https://iblockify.wordpress.com/ Link to comment Share on other sites More sharing options...
jaberwacky Posted January 18, 2014 Share Posted January 18, 2014 Since you seem to know the size of the array beforehand: #include <Array.au3> Global $string[3] = [''] For $i = 1 to 3 $string[$i - 1] = "test" & $i Next _ArrayDisplay($string) Not too much different. Just cleaned up some. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
AutID Posted January 18, 2014 Author Share Posted January 18, 2014 (edited) Since you seem to know the size of the array beforehand: #include <Array.au3> Global $string[3] = [''] For $i = 1 to 3 $string[$i - 1] = "test" & $i Next _ArrayDisplay($string) Not too much different. Just cleaned up some. I dont know the size of the array. Depends on the runs. I want to turn a string to array. Not declare an array and then add to it. Edited January 18, 2014 by AutID https://iblockify.wordpress.com/ Link to comment Share on other sites More sharing options...
czardas Posted January 18, 2014 Share Posted January 18, 2014 Use StringSplit. ; #include <Array.au3> Local $sString = "", $iMax = 10, $sDelim = "," For $i = 1 To $iMax $sString &= "test" & $i & $sDelim Next $sString = StringTrimRight($sString, 1) Local $aArray = StringSplit($sString, $sDelim, 2) _ArrayDisplay($aArray) robertocm 1 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
AutID Posted January 18, 2014 Author Share Posted January 18, 2014 (edited) Use StringSplit. ; #include <Array.au3> Local $sString = "", $iMax = 10, $sDelim = "," For $i = 1 To $iMax $sString &= "test" & $i & $sDelim Next $sString = StringTrimRight($sString, 1) Local $aArray = StringSplit($sString, $sDelim, 2) _ArrayDisplay($aArray) I thought about that solution as well but then i will have to add a seperator everytime. Not that it is a big deal but i was just wondering if there are other way than these two here. Edit: please don't feel offended. Thank you and everyone for helping me. I just want to see other solution as well. Edited January 18, 2014 by AutID https://iblockify.wordpress.com/ Link to comment Share on other sites More sharing options...
czardas Posted January 18, 2014 Share Posted January 18, 2014 If you know a repeating pattern will occur, then a third way would be to use regular expressions. In this case you are using a test string which is not what you will use in every case. It is not possible to create a general solution. A regular expression can often be written in different ways. ; #include <Array.au3> Local $sString = "test1test2test3" Local $aArray = StringRegExp($sString, "(?i)([a-z]+[0-9]+)", 3) _ArrayDisplay($aArray) jaberwacky 1 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
jaberwacky Posted January 18, 2014 Share Posted January 18, 2014 Nice one, I had gotten this and now I see why it doesn't work: #include <Array.au3> Global Const $string = "Test1Test2Test3" Global Const $array = StringRegExp($string, "(?i)(\w+\d+)", $STR_REGEXPARRAYMATCH) _ArrayDisplay($array) Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
czardas Posted January 18, 2014 Share Posted January 18, 2014 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
AutID Posted January 18, 2014 Author Share Posted January 18, 2014 I can't use @replaces because string will not be the same. It is going to be random. https://iblockify.wordpress.com/ Link to comment Share on other sites More sharing options...
jaberwacky Posted January 18, 2014 Share Posted January 18, 2014 Urm, can you give us some examples of strings that you may come across? Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
czardas Posted January 18, 2014 Share Posted January 18, 2014 (edited) I can't use @replaces because string will not be the same. It is going to be random. You have to have some defining criteria to know where to split the string. I quite often split a string into equal length segments using a function I wrote: _StringEqualSplit(). The criteria here is the length of each split. Edited January 18, 2014 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
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