BillFrosby Posted December 10, 2011 Posted December 10, 2011 Hello, I'm using a function I found in the forums to split a string into an array where each index contains 1 character: Local $res = _SplitByChar($userInput, 1) Func _SplitByChar($userInput, $nChars) Local $Array = '' For $iCC = 1 To StringLen($userInput) Step $nChars $Array &= StringMid($userInput, $iCC, $nChars) & Chr(1) Next If $Array Then Return StringSplit(StringTrimRight($Array, 1), Chr(1)) Return SetError(1, 0, '') EndFunc Then based on the index and the character performing specific functions: Do If $res[$Char] = "." or $res[$Char] = ";" or $res[$Char] = "," Or $res[$Char] = ":" Then function() and trying to replace those characters using stringreplace: StringReplace(StringReplace(StringReplace(StringReplace($res[$Char], ":", "-"), ";", "-"), ".", "-"), ",", "-") So that next loop through the array I should have a different command based on the new character. Until $b = $res[0] * 2 I'm not sure, but I think the problem is every time it calls $res[$char] it runs the _SplitByChar function and undoing my string replacements. I'm pretty new at this and it's probably something easy, but any help would be appreciated. Thanks
czardas Posted December 10, 2011 Posted December 10, 2011 (edited) To get one character in each element do this. #include <Array.au3> ; For _arrayDisplay Local $string = "12345abcde" Local $aArray = StringSplit($string, "", 2) _arrayDisplay($aArray) I didn't understand the rest of the question. Perhaps you could explain it better. Edited December 10, 2011 by czardas operator64 ArrayWorkshop
BillFrosby Posted December 10, 2011 Author Posted December 10, 2011 That was a much better way to split the string, thank you. Essentially I need to replace characters in the array. Ex. If aArray[$char] is a colon I want to change it to a dash. I used stringreplace, but it doesn't seem to be working.
czardas Posted December 10, 2011 Posted December 10, 2011 (edited) #include <Array.au3> ; For _arrayDisplay Local $string = "12345:ab:cde" Local $aArray = StringSplit($string, "", 2) _arrayDisplay($aArray, "Before") Local $searchChar = ":", $replaceChar = "-" For $i = 0 To Ubound($aArray) -1 If $aArray[$i] = $searchChar Then $aArray[$i] = $replaceChar Next _arrayDisplay($aArray, "After") In this case you could also replace the characters in the string before creating the array. #include <Array.au3> ; For _arrayDisplay Local $string = "12345:ab:cde" $string = StringReplace($string, ":", "-") Local $aArray = StringSplit($string, "", 2) _arrayDisplay($aArray, "Alternative Method") Edited December 10, 2011 by czardas operator64 ArrayWorkshop
BillFrosby Posted December 10, 2011 Author Posted December 10, 2011 (edited) I actually need the characters in the array. The character along with the position in the string tell tell the program what to do. The character position in the string tells the mouse where to move to, and the character tells it which function to carry out. So I want it to carry out one function, change the character, and perform a second different function on the next loop.Your first example worked, but I have to replace them all at once at the end of the first loop through the string. I have another question about it though.About this one:#include <Array.au3> ; For _arrayDisplayLocal $string = "12345:ab:cde"Local $aArray = StringSplit($string, "", 2)_arrayDisplay($aArray, "Before")Local $searchChar = ":", $replaceChar = "-"For $i = 0 To Ubound($aArray) -1 If $aArray[$i] = $searchChar Then $aArray[$i] = $replaceCharNext_arrayDisplay($aArray, "After")Can I use multiple characters, instead of just colon to dash, I would like to change colon, semicolon, comma, and period to a dash. Edited December 10, 2011 by BillFrosby
czardas Posted December 10, 2011 Posted December 10, 2011 (edited) Not much time today - in between music lessons. Try this. #include <Array.au3> ; For _arrayDisplay Local $string = "12,.3;45:ab:cde" Local $aArray = StringSplit($string, "", 2) _arrayDisplay($aArray, "Before") For $i = 0 To Ubound($aArray) -1 $aArray[$i] = StringRegExpReplace($aArray[$i], "[,:;.]", "-") Next _arrayDisplay($aArray, "After") Also try putting the code you post inside AutoIt code tags like this: [ autoit ] code [ /autoit ] only without the spaces. Edited December 10, 2011 by czardas operator64 ArrayWorkshop
BillFrosby Posted December 10, 2011 Author Posted December 10, 2011 Sorry for the delayed response, I fell asleep. I will start using autoit tags in the future. That worked, thank you for all your help.
czardas Posted December 10, 2011 Posted December 10, 2011 (edited) No worries, and I forgot to mention - welcome to the forum. Familiarize yourself with forum etiquette, and you will find that there are many here who will be willing to offer help when you need it. BTW I liked your concept. Edited December 10, 2011 by czardas operator64 ArrayWorkshop
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