Page 1 of 1
Char matching
Posted: Mon Nov 08, 2021 7:30 pm
by richmond62
I've got badly stuck . . .
Let's suppose I have a field that contains the word "cheese"
and I want to replace the instances of 'e' with 'x'.
So I set up a button with the following sort of code:
Code: Select all
on mouseUp
put "e" into BUKVA
put "x" into BUKVA2
put 1 into CHEQUE
repeat until char CHEQUE of fld "fRWORD" is empty
put char CHEQUE of fld "fRWORD" into VOX
if BUKVA is VOX then
replace (char CHEQUE of fld "fRWORD") with BUKVA2 in fld "fRWORD"
end if
add 1 to CHEQUE
end repeat
end mouseUp
This does NOT work . . .
I suspect the problem may lie with
Re: Char matching
Posted: Mon Nov 08, 2021 7:40 pm
by jmburnod
Hi Richmond,
Works nice with this:
Code: Select all
put "e" into BUKVA
put "x" into BUKVA2
instead this
Code: Select all
put 'e' into BUKVA
put 'x' into BUKVA2
Kind regards
Jean-Marc
Re: Char matching
Posted: Mon Nov 08, 2021 7:40 pm
by Klaus
Why not just:
Code: Select all
on mouseUp
replace "e" with "x" in fld "fRWORD"
end museup
Re: Char matching
Posted: Mon Nov 08, 2021 7:42 pm
by richmond62
Oddly enough, moved to another stack that seems to work.
Re: Char matching
Posted: Mon Nov 08, 2021 7:49 pm
by richmond62
Why not just:
Because what I am trying to do is this:
-
-
the result is that "----------" gets replaced by a string of aaaa s.

Re: Char matching
Posted: Mon Nov 08, 2021 7:55 pm
by richmond62
Thanks Jean Marc: I was SILLY using those single quotes,
but, as you can see, that was NOT enough.

Re: Char matching
Posted: Mon Nov 08, 2021 8:41 pm
by richmond62
Aha: well I see what is wrong:
this:
Code: Select all
on mouseUp
replace char 3 of fld "fRWORD2" with "X" in fld "fRWORD2"
end mouseUp
replaces ALL the characters in the field with "X" . . .
Which is NBG!
And seems to go against all the other TEXT manipulation stuff in LiveCode.
Re: Char matching
Posted: Mon Nov 08, 2021 9:31 pm
by dunbarx
Richmond.
Cannot be true. Tested here, and only char 3 of the field gets the "X".
As it certainly ought to.
Craig
Re: Char matching
Posted: Tue Nov 09, 2021 7:33 am
by richmond62
Not "over here".
Re: Char matching
Posted: Tue Nov 09, 2021 9:25 am
by bn
richmond62 wrote: ↑Mon Nov 08, 2021 8:41 pm
Aha: well I see what is wrong:
this:
Code: Select all
on mouseUp
replace char 3 of fld "fRWORD2" with "X" in fld "fRWORD2"
end mouseUp
replaces ALL the characters in the field with "X" . . .
Which is NBG!
And seems to go against all the other TEXT manipulation stuff in LiveCode.
It does exactly what you told LC to do:
from the dictionary:
replace in field
Type command
Syntax
replace oldString with newString in fieldContainer {preserving | replacing} styles
Your code:
evaluates to "oldString" in your case "-" and replaces all occurences of "oldString" with "newString" in your case "X"
Kind regards
Bernd
Re: Char matching
Posted: Tue Nov 09, 2021 10:19 am
by richmond62
It does exactly what you told LC to do:
That's normally what computers do.
Gottit:
Code: Select all
on mouseUp
put "a" into char 5 of fld "fRWORD2"
end mouseUp
Re: Char matching
Posted: Tue Nov 09, 2021 3:38 pm
by dunbarx
Richmond.
I now see what you are talking about. I made my test with a handful of random characters, not a string of identical characters.
But in any reasonable body of text, you will surely get some repeats, and so the direct "put someChar into" is the way to go.
Craig
Re: Char matching
Posted: Tue Nov 09, 2021 3:41 pm
by richmond62
Have a look at my "Hangman 4" stack over here:
viewtopic.php?f=25&t=36434&start=15
and see what happens.