Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
richmond62
- Livecode Opensource Backer

- Posts: 10098
- Joined: Fri Feb 19, 2010 10:17 am
Post
by richmond62 » Mon Nov 08, 2021 7:30 pm
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
-
jmburnod
- VIP Livecode Opensource Backer

- Posts: 2729
- Joined: Sat Dec 22, 2007 5:35 pm
-
Contact:
Post
by jmburnod » Mon Nov 08, 2021 7:40 pm
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
https://alternatic.ch
-
Klaus
- Posts: 14194
- Joined: Sat Apr 08, 2006 8:41 am
-
Contact:
Post
by Klaus » Mon Nov 08, 2021 7:40 pm
Why not just:
Code: Select all
on mouseUp
replace "e" with "x" in fld "fRWORD"
end museup
-
richmond62
- Livecode Opensource Backer

- Posts: 10098
- Joined: Fri Feb 19, 2010 10:17 am
Post
by richmond62 » Mon Nov 08, 2021 7:42 pm
Oddly enough, moved to another stack that seems to work.
-
richmond62
- Livecode Opensource Backer

- Posts: 10098
- Joined: Fri Feb 19, 2010 10:17 am
Post
by richmond62 » Mon Nov 08, 2021 7:49 pm
Why not just:
Because what I am trying to do is this:
-
-
the result is that "----------" gets replaced by a string of aaaa s.

-
richmond62
- Livecode Opensource Backer

- Posts: 10098
- Joined: Fri Feb 19, 2010 10:17 am
Post
by richmond62 » Mon Nov 08, 2021 7:55 pm
Thanks Jean Marc: I was SILLY using those single quotes,
but, as you can see, that was NOT enough.

-
richmond62
- Livecode Opensource Backer

- Posts: 10098
- Joined: Fri Feb 19, 2010 10:17 am
Post
by richmond62 » 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.
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Mon Nov 08, 2021 9:31 pm
Richmond.
Cannot be true. Tested here, and only char 3 of the field gets the "X".
As it certainly ought to.
Craig
-
bn
- VIP Livecode Opensource Backer

- Posts: 4171
- Joined: Sun Jan 07, 2007 9:12 pm
Post
by bn » Tue Nov 09, 2021 9:25 am
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
-
richmond62
- Livecode Opensource Backer

- Posts: 10098
- Joined: Fri Feb 19, 2010 10:17 am
Post
by richmond62 » Tue Nov 09, 2021 10:19 am
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
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Tue Nov 09, 2021 3:38 pm
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