repeat with ... step behaviour
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
repeat with ... step behaviour
..........
Last edited by [-hh] on Wed Aug 13, 2014 1:22 pm, edited 1 time in total.
shiftLock happens
Re: repeat with ... step behaviour
Hallo Hermann,
I was bitten often enough by this. I don't know if it is correct or not.
assuming repeat from j = 1 to 20 step 2
On one side you tell the repeat loop do a step unless you reach 20, so at 19 it will make a step and the result is 21
On the other side you could say: I told you 20 is my upper limit and I don't want any steps beyond 20 which would give you 19 as the last result.
I do routinely add a lower limit.
If the engine should do this?
I don't know of any circumstances where you would want to overshoot the result your upper limit, but you never know if anybody is using this behavior.
Actually I use step almost exclusively to step through imageData in steps of 4. And length of imageData is always a multiple of four and I start at 1
so I use
That works.
But when you raised the point I think a general solution is the one I posted first
This may be something for the use-list.
Kind regards
Bernd
I was bitten often enough by this. I don't know if it is correct or not.
assuming repeat from j = 1 to 20 step 2
On one side you tell the repeat loop do a step unless you reach 20, so at 19 it will make a step and the result is 21
On the other side you could say: I told you 20 is my upper limit and I don't want any steps beyond 20 which would give you 19 as the last result.
I do routinely add a lower limit.
Code: Select all
-- N is any integer > 1
-- x is any positive real number
repeat with j=1 to (N -x+1) step x
I don't know of any circumstances where you would want to overshoot the result your upper limit, but you never know if anybody is using this behavior.
Actually I use step almost exclusively to step through imageData in steps of 4. And length of imageData is always a multiple of four and I start at 1
so I use
Code: Select all
-- N is any integer > 1
-- x is any positive real number
repeat with j=1 to (N -x) step x
But when you raised the point I think a general solution is the one I posted first
This may be something for the use-list.
Kind regards
Bernd
Last edited by bn on Fri Apr 04, 2014 11:25 pm, edited 1 time in total.
Re: repeat with ... step behaviour
Hi,
I've explained how this seems to work on page 66 of my book. It looks like LiveCode first checks whether j<N and then adds x. So, Bernd is right about his general solution. Unfortunately, the general solution doesn't work for step 1. Usually, I just do a check, e.g. that a variable isn't empty or that line j of a field contains a number etc., before continuing the repeat loop.
Kind regards,
Mark
I've explained how this seems to work on page 66 of my book. It looks like LiveCode first checks whether j<N and then adds x. So, Bernd is right about his general solution. Unfortunately, the general solution doesn't work for step 1. Usually, I just do a check, e.g. that a variable isn't empty or that line j of a field contains a number etc., before continuing the repeat loop.
Kind regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: repeat with ... step behaviour
Hi Mark,
that works also for step 1. Or I don't understand what you are saying.
It is debatable if step 1 makes sense since it would be easier to code
Kind regards
Bernd
my "general solution" isUnfortunately, the general solution doesn't work for step 1
Code: Select all
-- N is any integer > 1
-- x is any positive real number
repeat with j=1 to (N -x+1) step x
It is debatable if step 1 makes sense since it would be easier to code
Code: Select all
repeat with j = 1 to N
Bernd
Re: repeat with ... step behaviour
I think I misunderstood "above", Bernd
You're right, what you do there works.
Mark

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: repeat with ... step behaviour
Hi Mark,
I get it. After rereading my post I also found it to be ambigous as to which code snippet "above" referred to and I changed it to "first"
Kind regards
Bernd
I get it. After rereading my post I also found it to be ambigous as to which code snippet "above" referred to and I changed it to "first"
Kind regards
Bernd
Re: repeat with ... step behaviour
..........
Last edited by [-hh] on Wed Aug 13, 2014 1:17 pm, edited 1 time in total.
shiftLock happens
Re: repeat with ... step behaviour
Hi Herman,
And they also know if there is any "off-label" use of this quirk that would break code if this would be changed on the engine side.
And I agree, the dictionary should have a note on this particular case of the repeat form. I think everybody that uses step for the first time expects that the repeat loop stops at the upper limit N
repeat with i = 1 to N step x
I tried my solution
and it works. Maybe the engine is doing some magic behind the scene.
Kind regards
Bernd
Edit: I have added a comment to the dictionary and it technically went fine. I has to be approved to show up in the dictionary for "repeat"
From what I see on the use-list there are more long time Metacard-Revolution-Livecode users which have a very good understanding of the language and how it evolved.bn wrote:
This may be something for the use-list.
Herman wrote:
I read a few times in there. That's why I'm surprised: What do you think is the difference from the use-list to here?
And they also know if there is any "off-label" use of this quirk that would break code if this would be changed on the engine side.
And I agree, the dictionary should have a note on this particular case of the repeat form. I think everybody that uses step for the first time expects that the repeat loop stops at the upper limit N
repeat with i = 1 to N step x
I tried my solution
Code: Select all
put 2 into N
put 5 into x
repeat with i = 1 to (N-x+1) step x
Kind regards
Bernd
Edit: I have added a comment to the dictionary and it technically went fine. I has to be approved to show up in the dictionary for "repeat"
Re: repeat with ... step behaviour
..........
Last edited by [-hh] on Wed Aug 13, 2014 1:17 pm, edited 1 time in total.
shiftLock happens
Re: repeat with ... step behaviour
Hi,
This script
works because LC first starts at 1 (as specified). It will always run the first time and only the second time it will check if the counter is within the specified range. However, there is something strange going on. Try this:
It won't work as long as N+1>=0. It works if you change i=2 into i=1. I don't have a logical explanation for this. Apparently, LC does a few unrelated checks depending on both the starting value and the specified range. I'm tempted to look at the source code now 
Kind regards,
Mark
This script
Code: Select all
repeat with i=1 to 0 step (N+1)
put i after s
end repeat
put s
Code: Select all
on mouseUp
repeat with i=2 to -1 step (2+1)
put i after s
end repeat
put s
end mouseUp

Kind regards,
Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: repeat with ... step behaviour
Hello, Hermann.
Craig
Craig
Re: repeat with ... step behaviour
..........
Last edited by [-hh] on Wed Aug 13, 2014 1:17 pm, edited 1 time in total.
shiftLock happens
Re: repeat with ... step behaviour
Hi,
HC had no "step" variant for its "repeat" structures. You had to manage this by hand, as you said, initializing a variable and incrementing it explicitly within the loop. The same "step-over" issues would have come up, though, and the compact solution that Bernd gave is just the ticket.
I am glad you mentioned fractional increments. This is something that needs attention. You are just the person to do it.
So glad to hear you again
Craig
HC had no "step" variant for its "repeat" structures. You had to manage this by hand, as you said, initializing a variable and incrementing it explicitly within the loop. The same "step-over" issues would have come up, though, and the compact solution that Bernd gave is just the ticket.
I am glad you mentioned fractional increments. This is something that needs attention. You are just the person to do it.
So glad to hear you again

Craig