repeat with if

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Fermin
Posts: 149
Joined: Fri Jun 05, 2015 10:44 pm

repeat with if

Post by Fermin » Fri Feb 12, 2016 3:58 pm

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

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: repeat with if

Post by Klaus » Fri Feb 12, 2016 4:11 pm

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: repeat with if

Post by Thierry » Fri Feb 12, 2016 4:15 pm

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
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: repeat with if

Post by Thierry » Fri Feb 12, 2016 4:16 pm

Hallo Klaus,

Once again you're faster :)
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Fermin
Posts: 149
Joined: Fri Jun 05, 2015 10:44 pm

Re: repeat with if

Post by Fermin » Fri Feb 12, 2016 4:36 pm

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.

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: repeat with if

Post by Thierry » Fri Feb 12, 2016 4:45 pm

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?
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

Fermin
Posts: 149
Joined: Fri Jun 05, 2015 10:44 pm

Re: repeat with if

Post by Fermin » Fri Feb 12, 2016 7:03 pm

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

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7393
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: repeat with if

Post by jacque » Sat Feb 13, 2016 5:59 pm

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.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply