Page 1 of 1
repeat with ... step behaviour
Posted: Fri Apr 04, 2014 2:32 pm
by [-hh]
..........
Re: repeat with ... step behaviour
Posted: Fri Apr 04, 2014 9:05 pm
by bn
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.
Code: Select all
-- N is any integer > 1
-- x is any positive real number
repeat with j=1 to (N -x+1) step x
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
Code: Select all
-- N is any integer > 1
-- x is any positive real number
repeat with j=1 to (N -x) step x
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
Re: repeat with ... step behaviour
Posted: Fri Apr 04, 2014 11:07 pm
by Mark
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
Re: repeat with ... step behaviour
Posted: Fri Apr 04, 2014 11:18 pm
by bn
Hi Mark,
Unfortunately, the general solution doesn't work for step 1
my "general solution" is
Code: Select all
-- N is any integer > 1
-- x is any positive real number
repeat with j=1 to (N -x+1) step x
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
Re: repeat with ... step behaviour
Posted: Fri Apr 04, 2014 11:21 pm
by Mark
I think I misunderstood "above", Bernd

You're right, what you do there works.
Mark
Re: repeat with ... step behaviour
Posted: Fri Apr 04, 2014 11:28 pm
by bn
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
Re: repeat with ... step behaviour
Posted: Fri Apr 04, 2014 11:32 pm
by [-hh]
..........
Re: repeat with ... step behaviour
Posted: Sat Apr 05, 2014 12:02 am
by bn
Hi Herman,
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?
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.
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
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"
Re: repeat with ... step behaviour
Posted: Sat Apr 05, 2014 1:30 am
by [-hh]
..........
Re: repeat with ... step behaviour
Posted: Sat Apr 05, 2014 11:45 am
by Mark
Hi,
This script
Code: Select all
repeat with i=1 to 0 step (N+1)
put i after s
end repeat
put s
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:
Code: Select all
on mouseUp
repeat with i=2 to -1 step (2+1)
put i after s
end repeat
put s
end mouseUp
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
Re: repeat with ... step behaviour
Posted: Sat Apr 05, 2014 2:51 pm
by dunbarx
Hello, Hermann.
Craig
Re: repeat with ... step behaviour
Posted: Sat Apr 05, 2014 4:49 pm
by [-hh]
..........
Re: repeat with ... step behaviour
Posted: Sun Apr 06, 2014 3:35 am
by dunbarx
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