Multiple istances of values in a repeat loop

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
trevix
Posts: 992
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Multiple istances of values in a repeat loop

Post by trevix » Wed Sep 04, 2024 5:48 pm

Hello!
Given this code, somebody can explain me why the number "2" gets repeated twice?

Code: Select all

local sNumber = 2

on mouseUp pMouseButton
     put sNumber mod 2 + 1 into sNumber --alternate 1 and 2 at each press
    MofifyTheRepeat
end mouseUp

On MofifyTheRepeat
     repeat 5
          put sNumber & " " after msg
          if sNumber = 2 then exit repeat
          wait 3 seconds with messages
     end repeat
     beep
end MofifyTheRepeat
How to do it:
- run mouseup once
- run it again after a few seconds
EXPECTED RESULT: on the second press, only one "2" should appear on the msg
OBSERVED RESULT: two occurences of the "2" appear on the msg.

Is there a way to avoid this?
Thanks
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

trevix
Posts: 992
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Multiple istances of values in a repeat loop

Post by trevix » Wed Sep 04, 2024 6:16 pm

This interesting too (but not solving):

Code: Select all

local sNumber = 2

on mouseUp pMouseButton
     put sNumber mod 2 + 1 into sNumber
    send "MofifyTheRepeat sNumber" to me in 0 seconds
end mouseUp

On MofifyTheRepeat pValue
     put 0 into tCounter
     repeat 5
          add 1 to tCounter
          if pValue = 2 then 
               put "(" & tCounter & ")" & pValue & " " after msg
               beep
               exit repeat
          end if
          put "(" & tCounter & ")" & pValue & " " after msg
          wait 3 seconds with messages
     end repeat
end MofifyTheRepeat
So, my point is: how to stop, from a script outside the repeat, ALL instances of a loop? (even exit to top doesn't work).
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

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

Re: Multiple istances of values in a repeat loop

Post by dunbarx » Wed Sep 04, 2024 6:41 pm

Trevix.

sNumber changes in the "MofifyTheRepeat" (sic) handler. First it is "2", and then "1".

Craig

trevix
Posts: 992
Joined: Sat Feb 24, 2007 11:25 pm
Contact:

Re: Multiple istances of values in a repeat loop (SOLVED)

Post by trevix » Wed Sep 04, 2024 6:51 pm

I got it:

Code: Select all

local sNumber = 2
local sFlag
local sCounter

on mouseUp pMouseButton
     put sNumber mod 2 + 1 into sNumber --alternate 1 and 2 at each press
     if sNumber = 1 then  
          put empty into msg
          put 0 into sCounter
          put false into sFlag
     else
          put true into sFlag
     end if
     send "MofifyTheRepeat sNumber" to me in 0 seconds
end mouseUp

On MofifyTheRepeat pValue
     repeat 5
          add 1 to sCounter
          if pValue = 2  OR sFlag then 
               put "(" & sCounter & ")" & pValue & " " after msg
               beep
               exit repeat
          end if
          put "(" & sCounter & ")" & pValue & " " after msg
          wait 3 seconds with messages
          if sFlag then exit repeat
     end repeat
     put 2 into sNumber
end MofifyTheRepeat
Trevix
OSX 14.3.1 xCode 15 LC 10 DP7 iOS 15> Android 7>

Post Reply

Return to “Getting Started with LiveCode - Experienced Developers”