repeat with ... step behaviour

Want to talk about something that isn't covered by another category?

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

repeat with ... step behaviour

Post by [-hh] » Fri Apr 04, 2014 2:32 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 1:22 pm, edited 1 time in total.
shiftLock happens

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: repeat with ... step behaviour

Post by bn » Fri Apr 04, 2014 9:05 pm

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
Last edited by bn on Fri Apr 04, 2014 11:25 pm, edited 1 time in total.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: repeat with ... step behaviour

Post by Mark » Fri Apr 04, 2014 11:07 pm

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
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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: repeat with ... step behaviour

Post by bn » Fri Apr 04, 2014 11:18 pm

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

Code: Select all

repeat with j = 1 to N
Kind regards
Bernd

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: repeat with ... step behaviour

Post by Mark » Fri Apr 04, 2014 11:21 pm

I think I misunderstood "above", Bernd :-) You're right, what you do there works.

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: repeat with ... step behaviour

Post by bn » Fri Apr 04, 2014 11:28 pm

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

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: repeat with ... step behaviour

Post by [-hh] » Fri Apr 04, 2014 11:32 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 1:17 pm, edited 1 time in total.
shiftLock happens

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4171
Joined: Sun Jan 07, 2007 9:12 pm

Re: repeat with ... step behaviour

Post by bn » Sat Apr 05, 2014 12:02 am

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"

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: repeat with ... step behaviour

Post by [-hh] » Sat Apr 05, 2014 1:30 am

..........
Last edited by [-hh] on Wed Aug 13, 2014 1:17 pm, edited 1 time in total.
shiftLock happens

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: repeat with ... step behaviour

Post by Mark » Sat Apr 05, 2014 11:45 am

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
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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: repeat with ... step behaviour

Post by dunbarx » Sat Apr 05, 2014 2:51 pm

Hello, Hermann.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm

Re: repeat with ... step behaviour

Post by [-hh] » Sat Apr 05, 2014 4:49 pm

..........
Last edited by [-hh] on Wed Aug 13, 2014 1:17 pm, edited 1 time in total.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10317
Joined: Wed May 06, 2009 2:28 pm

Re: repeat with ... step behaviour

Post by dunbarx » Sun Apr 06, 2014 3:35 am

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 :D

Craig

Post Reply