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
-
JoeE.
- Posts: 12
- Joined: Tue Apr 27, 2010 5:33 pm
Post
by JoeE. » Tue Jul 05, 2022 4:55 pm
Hi everyone at the forum,
is there a built-in function to detect the position of a character (or item, word...) in a "for-each" loop? I couldn't find anything about this...
Example:
Code: Select all
put "the quick brown fox" into myVariable
repeat for each character dummy in myVariable
--get the position of dummy in the string myVariable here
end repeat
Thanks very much for your help

Joerg
-
jmburnod
- VIP Livecode Opensource Backer

- Posts: 2729
- Joined: Sat Dec 22, 2007 5:35 pm
-
Contact:
Post
by jmburnod » Tue Jul 05, 2022 5:08 pm
Hi,
I would use a counter.
Code: Select all
...
put 0 into tCount
put empty into tBuff
put "the quick brown fox" into myVariable
repeat for each character dummy in myVariable
add 1 to tCount
put dummy & "," & tCount & cr after tBuff
--get the position of dummy in the string myVariable here
end repeat
delete char -1 of tBuff
put tBuff
...
Best regards
Jean-Marc
https://alternatic.ch
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Tue Jul 05, 2022 6:34 pm
Hi.
Jean-Marc has offered one way. Here are two more. On a new card make a button and a field, and put all of "the quick brown...." in the field. Now put this in the button script:
Code: Select all
on mouseUp
get fld 1
put 0 into charNum
repeat the number of chars of it
put offset ("e",it,charNum)into foundNum
add foundNum to charNum
if foundNum = 0 then
answer accum
exit to top
end if
put charNum & comma after accum
end repeat
end mouseUp
This is all instances of the letter 'e".
Craig
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Tue Jul 05, 2022 6:38 pm
Oh yes, the other way.
Just use the "repeat with" construction. That contains a running counter built-in. Same as before, but with this in the button script:
Code: Select all
on mouseUp
get fld 1
repeat with y = 1 to the number of chars of it
if char y of it = "e" then put y & comma after accum
end repeat
answer accum
end mouseUp
This seems clunkier, because it goes through each char one by one.
Craig
-
JoeE.
- Posts: 12
- Joined: Tue Apr 27, 2010 5:33 pm
Post
by JoeE. » Wed Jul 06, 2022 6:21 am
Hi Jean-Marc and Craig,
Thanks very much for taking the time! I think, I will play around with your solutions. It's a bit tricky, because I want to use some nested loops, so I hope not to get lost in the code
Thanks again!
Joerg
-
jmburnod
- VIP Livecode Opensource Backer

- Posts: 2729
- Joined: Sat Dec 22, 2007 5:35 pm
-
Contact:
Post
by jmburnod » Wed Jul 06, 2022 9:56 am
I want to use some nested loops, so I hope not to get lost in the code
You may use a function to make your code clearer
Jean-Marc
https://alternatic.ch
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Wed Jul 06, 2022 1:23 pm
Hi.
Why do you "want" to use nested loops? For practice?
If so, by all means, but simpler is better, especially if you want to understand what you wrote in two months.
Craig
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Wed Jul 06, 2022 1:25 pm
Hi again.
Do you know how to place breakpoints in a script? If so, try them with all three offerings, and watch the values of the several variables step by step. This will not take long, and you can see what is going on.
If not, write back.
Craig
-
JoeE.
- Posts: 12
- Joined: Tue Apr 27, 2010 5:33 pm
Post
by JoeE. » Wed Jul 06, 2022 2:00 pm
Hi Craig,
please excuse me for not being able to express myself so good in English
I know how to use breakpoints, yes. I have to iterate through some fields with text or values. The current position of the respective loop does matter. So it's not for practice

Would be nice, if this was built-in natively into LiveCode, with a 2nd parameter or so, like "repeat for each item dummy,[loop pos] in field..." I think in some situations, this could be useful.
Best regards,
Joerg
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Wed Jul 06, 2022 5:37 pm
OK, and using the "repeat with..." variant does not do that for you?
Otherwise, as Jean-Marc mentioned, you can write a function and keep it forever.
Craig
-
FourthWorld
- VIP Livecode Opensource Backer

- Posts: 10045
- Joined: Sat Apr 08, 2006 7:05 am
-
Contact:
Post
by FourthWorld » Wed Jul 06, 2022 5:41 pm
JoeE. wrote: ↑Wed Jul 06, 2022 2:00 pm
I have to iterate through some fields with text or values. The current position of the respective loop does matter. So it's not for practice

Would be nice, if this was built-in natively into LiveCode, with a 2nd parameter or so, like "repeat for each item dummy,[loop pos] in field..." I think in some situations, this could be useful.
If you could show us a sample of the input data and an example of what you'd like the output to look like, we can help you get from one to the other easily.
-
richmond62
- Livecode Opensource Backer

- Posts: 10097
- Joined: Fri Feb 19, 2010 10:17 am
Post
by richmond62 » Wed Jul 06, 2022 6:28 pm
-
If you could show us a sample of the input data and an example of what you'd like the output to look like, we can help you get from one to the other easily.
Very much so.
-
Attachments
-
- WORDY.livecode.zip
- Stack.
- (43.33 KiB) Downloaded 155 times
-
bobcole
- VIP Livecode Opensource Backer

- Posts: 161
- Joined: Tue Feb 23, 2010 10:53 pm
Post
by bobcole » Fri Jul 08, 2022 3:59 am
Here is a stack with my code for determining character positions in a word or phrase.
Two nested loops. One is a "for-each" loop.
My two cents.
Bob
-
FourthWorld
- VIP Livecode Opensource Backer

- Posts: 10045
- Joined: Sat Apr 08, 2006 7:05 am
-
Contact:
Post
by FourthWorld » Fri Jul 08, 2022 5:07 am
JoeE. wrote: ↑Tue Jul 05, 2022 4:55 pm
Hi everyone at the forum,
is there a built-in function to detect the position of a character (or item, word...) in a "for-each" loop? I couldn't find anything about this...
As you've seen, there are a great many ways to do things like this.
If you'll tell us a bit about
why you're doing this, we can help you find the one that best fits your needs.
What should the output look like?
-
dunbarx
- VIP Livecode Opensource Backer

- Posts: 10317
- Joined: Wed May 06, 2009 2:28 pm
Post
by dunbarx » Fri Jul 08, 2022 5:39 am
bobcole
The OP wanted to know the position()s of a certain char in a particular body of text, not the positions of the chars in the text itself.
In any case, since Richard is touting the many ways to do things, what do you think about this:
Code: Select all
put "Quick Brown Aardvark" into rawText
replace space with "" in rawText -- do not count spaces
repeat with y = 1 to the number of chars in rawText
put "Position" && y & comma && char y of rawText & return after accum
end repeat
answer accum
Craig