Page 1 of 1

repeat with if

Posted: Fri Feb 12, 2016 3:58 pm
by Fermin
Hi,
I need a loop (with many lines of code inside) acting in ascending or descending order depending on the state of an external button.
How do I do it without creating two loops duplicating all the code?... ie acting only on the head?

Something like this does not seem to work:
-------------
on mouseUp
if not the hilite of button "reverse" then repeat with a = 1 to 200
if the hilite of button "reverse" then repeat with a = 200 down to 1
## code
end repeat -- ???
end repeat -- ???
end mouseUp
-------------

Thank you

Re: repeat with if

Posted: Fri Feb 12, 2016 4:11 pm
by Klaus
Hola Fermin,

you cannot nest the loops this way :D
If I understood your problem correctly, this should do it:

Code: Select all

on mouseUp
   if the hilite of btn "reverse" then
      repeat with a = 200 down to 1
         ## do your reverse repeat stuff here...
      end repeat
   else
     repeat with a = 1 to 200
         ## do your repeat stuff here...
      end repeat
   end if
end mouseUp
Best

Klaus

Re: repeat with if

Posted: Fri Feb 12, 2016 4:15 pm
by Thierry
Well, one quick way:

Code: Select all

on mouseUp
   get the hilite of me button "reverse"
   if IT is true  then 
      repeat with a = 1 to 20
         yourcode a
      end repeat
   else
      repeat with a = 20 down to 1
         yourcode a
      end repeat
   end if
end mouseUp

on yourcode a
   # ...
end yourcode
HTH,

Thierry

Re: repeat with if

Posted: Fri Feb 12, 2016 4:16 pm
by Thierry
Hallo Klaus,

Once again you're faster :)

Re: repeat with if

Posted: Fri Feb 12, 2016 4:36 pm
by Fermin
Thanks, Thierry and Klaus, but the inner code is the same in both cases. I do not want to repeat it. I would like to work by changing just the first line.
[Repeat with a = 1 to 200] vs [repeat with a = 200 down to 1]

... but it may not be possible.

Re: repeat with if

Posted: Fri Feb 12, 2016 4:45 pm
by Thierry
Fermin wrote:Thanks, Thierry and Klaus, but the inner code is the same in both cases.
well, putting the inner code in a handler,
then you have only one time your code!

Code: Select all

on yourcode a
   # ...
end yourcode
And don't worry, it's fast :)

Or what did I miss?

Re: repeat with if

Posted: Fri Feb 12, 2016 7:03 pm
by Fermin
Good idea, but the handler must to have many local/global variables ...
I think I've found a solution that seems to serve:

Code: Select all

on mouseup
   -- the main variable is 'a'
   repeat with b = 1 to 200
      put b into a -- normal mode
      if the hilite of button "reverse" then put 200-b into a -- reverse mode
      -- here the code related with variable 'a'
   end repeat
end mouseup

Thank you very much

Re: repeat with if

Posted: Sat Feb 13, 2016 5:59 pm
by jacque
If the script produces any kind of list, it might be possible to run the handler normally and then reverse sort the list after the loop completes if the Reverse button is hilited.