Page 1 of 2
Position of character in a for-each loop
Posted: Tue Jul 05, 2022 4:55 pm
by JoeE.
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
Re: Position of character in a for-each loop
Posted: Tue Jul 05, 2022 5:08 pm
by jmburnod
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
Re: Position of character in a for-each loop
Posted: Tue Jul 05, 2022 6:34 pm
by dunbarx
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
Re: Position of character in a for-each loop
Posted: Tue Jul 05, 2022 6:38 pm
by dunbarx
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
Re: Position of character in a for-each loop
Posted: Wed Jul 06, 2022 6:21 am
by JoeE.
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
Re: Position of character in a for-each loop
Posted: Wed Jul 06, 2022 9:56 am
by jmburnod
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
Re: Position of character in a for-each loop
Posted: Wed Jul 06, 2022 1:23 pm
by dunbarx
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
Re: Position of character in a for-each loop
Posted: Wed Jul 06, 2022 1:25 pm
by dunbarx
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
Re: Position of character in a for-each loop
Posted: Wed Jul 06, 2022 2:00 pm
by JoeE.
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
Re: Position of character in a for-each loop
Posted: Wed Jul 06, 2022 5:37 pm
by dunbarx
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
Re: Position of character in a for-each loop
Posted: Wed Jul 06, 2022 5:41 pm
by FourthWorld
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.
Re: Position of character in a for-each loop
Posted: Wed Jul 06, 2022 6:28 pm
by richmond62
-
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.
Re: Position of character in a for-each loop
Posted: Fri Jul 08, 2022 3:59 am
by bobcole
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
Re: Position of character in a for-each loop
Posted: Fri Jul 08, 2022 5:07 am
by FourthWorld
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?
Re: Position of character in a for-each loop
Posted: Fri Jul 08, 2022 5:39 am
by dunbarx
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