repeat for each token and the character '\'

If you find an issue in LiveCode but are having difficulty pinning down a reliable recipe or want to sanity-check your findings with others, this is the place.

Please have one thread per issue, and try to summarize the issue concisely in the thread title so others can find related issues here.

Moderator: Klaus

Post Reply
rinzwind
Posts: 135
Joined: Tue May 01, 2012 10:44 am

repeat for each token and the character '\'

Post by rinzwind » Wed Jul 22, 2015 2:05 pm

The repeat loop exits as soon as it encounters a \ ... seems like an ugly bug to me?
Or am I missing some hidden feature?

put "1 2 3 4 5 \ 6 7 8 9 10" into test
repeat for each token t in test
put t & comma after msg
end repeat

rinzwind
Posts: 135
Joined: Tue May 01, 2012 10:44 am

Re: repeat for each token and the character '\'

Post by rinzwind » Wed Jul 22, 2015 2:12 pm

or -- or //
seems to quit on some live code script constructs

rinzwind
Posts: 135
Joined: Tue May 01, 2012 10:44 am

Re: repeat for each token and the character '\'

Post by rinzwind » Wed Jul 22, 2015 2:14 pm

any workaround? i was using token as this most closely resembles what I'm looking for...
word construct includes characters like (test
maybe customizable delimiters?
I hoped one could specify the tokens that 'token' sees as a delimiter... would be way more flexible.

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: repeat for each token and the character '\'

Post by Martin Koob » Wed Jul 22, 2015 2:29 pm

The "\" character is used to break a line in a script so you can have one long line of script shown on multiple shorter lines. Inside a quoted string it does not have that effect.

You are right it seems when tries to set t= "\" it exits the repeat loop. Perhaps the fact that this character is interpreted as a script line break is causing this somehow.

Martin

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: repeat for each token and the character '\'

Post by Martin Koob » Wed Jul 22, 2015 2:33 pm

You can set the itemDelimiter to a specific character and then repeat for each item.

Martin

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: repeat for each token and the character '\'

Post by Martin Koob » Wed Jul 22, 2015 2:42 pm

rinzwind wrote:or -- or //
seems to quit on some live code script constructs
'--' and '//' are used to comment out lines of script. So they are also characters that affect the parsing lines of script just like '\' . Another set of characters that does that is '/*' which is used with a following "*/" for commenting out blocks of code. Putting "/*" in your text variable will also cause the exit from the repeat loop.

So using any of these character sequences that are used for parsing scripts causes a problem. Not sure if it is a bug or if there is a workaround if you needed to use these characters as tokens.

Martin

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10045
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: repeat for each token and the character '\'

Post by FourthWorld » Wed Jul 22, 2015 2:49 pm

Because "\" is within a quoted string I would consider this a bug. Please feel free to file a bug report against this:
http://quality.runrev.com/
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: repeat for each token and the character '\'

Post by Martin Koob » Wed Jul 22, 2015 3:48 pm

It is interesting because when quoted in the string the '\' character (or '--' or '//' or '/*' does not pose a problem, that line executes normally.

It is when that '\' character is used by the repeat loop as one of the tokens in whatever data structure holds 'every token' that the issue occurs.

Martin

Martin Koob
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 256
Joined: Sun May 27, 2007 8:19 pm

Re: repeat for each token and the character '\'

Post by Martin Koob » Wed Jul 22, 2015 4:01 pm

Furthermore, this does not cause a problem when you use each 'item'.

If I make the string a comma separated list and repeat for each item there is not a problem.

put "1,2,3,4,5,\,6,7,8,9,10" into test
repeat for each item t in test
put t & comma after msg
end repeat

Nor does it affect each 'word'

put "1 2 3 4 5 \ 6 7 8 9 10" into test
repeat for each word t in test
put t & comma after msg
end repeat

Martin

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10045
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: repeat for each token and the character '\'

Post by FourthWorld » Wed Jul 22, 2015 4:41 pm

After reading Martin's last two posts I think he's onto something and that this isn't a bug after all. I had overlooked that the chunk type used is "token", which is very specialized and highly dependent on specifics within the deep innards of how the engine expects to encounter strings. Since all the elements are already space-delimited, the "word" chunk type is much simpler to use here.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1232
Joined: Thu Apr 11, 2013 11:27 am

Re: repeat for each token and the character '\'

Post by LCMark » Wed Jul 22, 2015 5:18 pm

@FourthWorld: You are correct - the 'token' chunk is tied to the notion of 'token' used internally by the script parser. In this case '\' is the 'next line continuation character' meaning - ignore all characters after this until the next newline and then continue processing after that. Since there is no newline in 'test', in this case it skips everything after '\' and finishes iterating.

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 194
Joined: Thu Apr 18, 2013 2:48 pm

Re: repeat for each token and the character '\'

Post by livecodeali » Wed Jul 22, 2015 5:19 pm

The idea of the token chunk (at least in its current hard-coded form) is that a token is a significant chunk with respect to parsing the LiveCode script language. Therefore for any of the following, you will see the number of tokens = 0, token n = empty:

"/* some text */"
"// some text"
"-- some text"
"# some text"

since these are all comments in LiveCode script and therefore not significant chunks. It is not that the repeat loop is cause to exit as such, merely that there are no further tokens so the repeat loop is complete.

In script, "\" is the line continuation character, and thus everything else on the current line is disregarded as far as the token chunk is concerned. The next token would be the first token of the next line. The following

put "1 2 3 4 5 \" & return & " 6 7 8 9 10" into test
repeat for each token t in test
put t & comma after msg
end repeat

would give you what you expected.

At some point we intend to introduce a configurable version of the token chunk, so that things can be tokenized in any way you like, but for now you're stuck with exactly how the LiveCode script parser interprets a token.

livecodeali
Livecode Staff Member
Livecode Staff Member
Posts: 194
Joined: Thu Apr 18, 2013 2:48 pm

Re: repeat for each token and the character '\'

Post by livecodeali » Wed Jul 22, 2015 5:27 pm

Heh, pipped by @LCMark :-)

Post Reply