Sign in to follow this  
Followers 0
jennico

StringReplace problem could be a bug ?

17 posts in this topic

#1 ·  Posted (edited)

hi,

took me an hour to finally find the error in my script.....

let's say, $a="1253000011111"

now i perform $a=StringReplace($a,5,"x"), because i want to replace the "5" in $a with "x".

but the result is: $a="1253x00011111" instead of $a="12x3000011111", what i expected.

(in fact, my script line is much more complicated, i just simplified it here.)

so, is there a trick to make the function work in my way ?

the helpfile says:

The substring to search for or the character position to start the replacement.

can i precisize this or do i have to make a workaround by splitting the string ?

and for the future: please update the logic of this function to disambiguate it.

thx

j.

Edited by jennico

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Share this post


Link to post
Share on other sites



It works fine for me. I suspect the problem is with your sig...

OS: Win.XP.SP2 AutIt Version: 3.2.8.1

:D


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

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

well, maybe i simplified my code too much. i have several algebraic functions in it. let me say it more basically:

what result would you expect:

$a=StringReplace("1253000011111",5,"x") ?

both results, $a="12x3000011111" and $a="1253x00011111" would be correct in the same way.

how can you drive the function to the one way or to the other ?

this is ambiguous and should be clarified, i think.

j.

edit:

the function has not changed from 3.2.8.1 to 3.2.10.0 and i can't work with the new version because too many bugs. but this is not the solution.

Edited by jennico

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

well, maybe i simplified my code too much. i have several algebraic functions in it. let me say it more basically:

what result would you expect:

$a=StringReplace("1253000011111",5,"x") ?

both results, $a="12x3000011111" and $a="1253x00011111" would be correct in the same way.

how can you drive the function to the one way or to the other ?

this is ambiguous and should be clarified, i think.

j.

edit:

the function has not changed from 3.2.8.1 to 3.2.10.0 and i can't work with the new version because too many bugs. but this is not the solution.

You need to quote the number 5 in the StringReplace call.

Straight from the help file...

searchstring - The substring to search for or the character position to start the replacement.

Edited by weaponx

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

what result would you expect:

$a=StringReplace("1253000011111",5,"x") ?

I'd expect $a="12x3000011111"

Since your telling StringReplace to look for a string in a string and replace it.

So your search string is 5 .. There is a 5 in the string to be searched .. It replaces the 5 with x ..

So it's doing exactly what it's meant to.

But if I wanted to go by position 5 instead of the search string 5 then I would use

$a="1253000011111"
$a=StringReplace($a, 5, "x", 1)

ConsoleWrite($a & @LF) ; 1253x00011111

Cheers

Edit: I didn't use "" around my 5 either and it works.

Edited by smashly

Share this post


Link to post
Share on other sites

Different version perhaps, doesn't matter. Just wrap the search string in quotes.

Share this post


Link to post
Share on other sites

$a=StringReplace("1253000011111",5,"x") ?

I'd expect $a="12x3000011111"

I don't know why would you expect to replace a substring when you don't actually specify any substring.

There are strings and there are numbers.

Substring being the string type, and position being the number type which makes sense.


"be smart, drink your wine"

Share this post


Link to post
Share on other sites

I thought it would be a case that if you wanted to search for a string the you'd put "" around your search string (to let it know it's a string)..

If you wanted to do it by position then use a number with no "" around it (so it knows to replace at a position).

But it doesn't seem to be the case

Side note:

I'm using Autoit 3.2.10.0 and 3.2.11.0 beta.

Share this post


Link to post
Share on other sites

well, okay then:

all you say is right.... but......:

this is my line:

$Zukunft="535000000000000xx"
For $i=4 To 21
If $i=11 Then $i=18
$Zukunft=StringReplace($Zukunft,$i+5*($i=9 Or $i=10)-9*($i>17),1)
next

this is a number: $i+5*($i=9 Or $i=10)-9*($i>17). not a string ! i also tried

Number($i+5*($i=9 Or $i=10)-9*($i>17)). same problem.

but instead of replacing the position, to my surprise the function replaces the searchstring.

j.


Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Share this post


Link to post
Share on other sites

You have to get it from an automatic float after calculation, to integer for this function to work;

CODE: AutoIt$Zukunft = StringReplace($Zukunft,Int($i + 5 * ($i = 9 Or $i = 10) - 9 * ($i > 17)), 1)Best, Randall

yes ! that's it ! :):):)

i did not think of "Int()", i only tried "Number()", which did not work. i could not imagine that the result is a float although it seems integer.

thank you indeed, randallc, you helped a lot.

j.


Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

i did not think of "Int()", i only tried "Number()", which did not work. i could not imagine that the result is a float although it seems integer.

It's appears to be a variable typing thang:

$sString = "0123"

$a = 1 + 1
ConsoleWrite("Debug: $a = " & $a & "  type = " & VarGetType($a) & "  IsInt($a) = " & IsInt($a) & @LF)
$sString_a = StringReplace($sString, $a, "x")
ConsoleWrite("Debug: $sString_a = " & $sString_a & @LF)

$b = Sqrt(4)
ConsoleWrite("Debug: $a = " & $b & "  type = " & VarGetType($b) & "  IsInt($b) = " & IsInt($b) & @LF)
$sString_b = StringReplace($sString, $b, "x")
ConsoleWrite("Debug: $sString_b = " & $sString_b & @LF)

Inside it's little C++ binary heart, StringReplace must be checking the type with something that doesn't recognize a Double (float) as a number/integer/whatever-its-looking-for.

:)

P.S. Added a Bug Trak, ticket #95.

Edited by PsaltyDS

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

Share this post


Link to post
Share on other sites

#14 ·  Posted (edited)

i see two mini-problems deep inside stringreplace's little heart:

1.

$i + 5 * ($i = 9 Or $i = 10) - 9 * ($i > 17)

is definitely an arithmetic operation (furthermore results in an integer) so stringreplace should not be able to interprete the result as a string.

2.

Number() should clarify the type of variable, but does not.

on the other hand, Int() works fine, so thanks to you all.

j.

Edited by jennico

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Share this post


Link to post
Share on other sites

#15 ·  Posted (edited)

i see two mini-problems deep inside stringreplace's little heart:

1.

$i + 5 * ($i = 9 Or $i = 10) - 9 * ($i > 17)

is definitely an arithmetic operation (furthermore results in an integer) so stringreplace should not be able to interprete the result as a string.

2.

Number() should clarify the type of variable, but does not.

on the other hand, Int() works fine, so thanks to you all.

j.

You are using booleans as numbers. Bad idea. Note the changing types of $x:

$i = 2
$x = ($i = 9 Or $i = 10)
ConsoleWrite("Debug: $x = " & $x & "  Type = " & VarGetType($x) & @LF)
$x = ($i > 17)
ConsoleWrite("Debug: $x = " & $x & "  Type = " & VarGetType($x) & @LF)
$x = $i + 5 * ($i = 9 Or $i = 10) - 9 * ($i > 17)
ConsoleWrite("Debug: $x = " & $x & "  Type = " & VarGetType($x) & @LF)

Don't lurk too far into AutoIt variable typing -- Here be dragons!

:)

Edited by PsaltyDS

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

Share this post


Link to post
Share on other sites

or, to say it with the Pretenders: "it's a thin line between number and string...." :)


Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

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