Sign in to follow this  
Followers 0
BillFrosby

Replace string in array trouble

8 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 _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")

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 by BillFrosby

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

#8 ·  Posted (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 by czardas

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0