Clearing local variables

Kay C Lan lan.kc.macmail at gmail.com
Wed Apr 27 21:31:37 EDT 2016


On Wed, Apr 27, 2016 at 11:32 PM, Bob Sneidar <bobsneidar at iotecdigital.com>
wrote:

>
> well... to be clear, you can in a repeat with x = 1 to n form.


Well to be really clear you NEED to be careful if you intend to modify x or
tVar in both cases:

repeat with x = 1 to the number of items in tVar
repeat for each item x in tVar

If you do not understand what isn't 'variable' and what really gets checked
at each iteration for each style of repeat then you are likely to see some
unexpected results. On the other hand, if do understand what is really
going on, then you can modify x to your advantage in either case. As for
tVar in the 'repeat for each' case, I believe Mark Waddingham has cautioned
that although your scripts might currently work if you modify tVar mid
repeat, LC is NOT guaranteed to behave the same way in the future.

Put the below script (see very bottom) in a button and step through it to
see:

Test 1
put "a,s,d,f" into tVar
repeat with x = 1 to the number of items in tVar
put empty into x
breakpoint
end repeat

--putting empty into x resets the count so this will never end.
--putting a non numeric value into x will cause an error

Test 2
put "a,s,d,f" into tVar
repeat for each item x in tVar
    put empty into x
end repeat

--whether empty or any value has no effect and will happily run as expected.

Test 3
put "a,s,d,f" into tVar
repeat with x = 1 to the number of items in tVar
    delete item 1 to x of tVar
end repeat

--you probably expect this to loop 3 times but it doesn't, it does an
unexpected 4th loop.

Test 4
put "a,s,d,f" into tVar
put 0 into tCount
repeat for each item x in tVar
    delete item 1 to (tCount + 1) of tVar
    add 1 to tCount
end repeat

--you probably expect this to loop 3 times but it doesn't, it does an
unexpected 4th loop.
--not guaranteed to behave this way in the future

Test 5
put "a,s,d,f" into tVar
repeat with x = 1 to the number of items in tVar
    put comma & item 1 to x of tVar after tVar
end repeat

--you probably expect this to run forever, but it doesn't

Test 6
put "a,s,d,f" into tVar
put 0 into tCount
repeat for each item x in tVar
    put comma & item 1 to (tCount + 1) of tVar after tVar
    add 1 to tCount
end repeat

--you probably expect this to run forever, but it doesn't
--not guaranteed to behave this way in the future


The whole script:
on mouseUp
   breakpoint
   put "a,s,d,f" into tVar
   put "Test 1" into msg
   put 0 into tCount
   repeat with x = 1 to the number of items in tVar
      put empty into x
      add 1 to tCount
      if tCount > 4 then
        put cr & "FAILED" after msg
        exit repeat
      end if
   end repeat
   put cr & "Final x: " & x after msg
   put cr & "Count: " & tCount after msg
   put cr & cr & "Test 2" after msg
   put 0 into tCount
   repeat for each item x in tVar
      put empty into x
      add 1 to tCount
   end repeat
   put cr & "Final x: " & x after msg
   put cr & "Count: " & tCount after msg
   put cr & cr & "Test 3" after msg
   put 0 into tCount
   breakpoint
   repeat with x = 1 to the number of items in tVar
      delete item 1 to x of tVar
      add 1 to tCount
   end repeat
   put cr & "Final x: " & x after msg
   put cr & "Count: " & tCount after msg
   put cr & cr & "Test 4" after msg
   put "a,s,d,f" into tVar
   put 0 into tCount
   breakpoint
   repeat for each item x in tVar
      delete item 1 to (tCount + 1) of tVar
      add 1 to tCount
   end repeat
   put cr & "Final x: " & x after msg
   put cr & "Count: " & tCount after msg
   put cr & cr & "Test 5" after msg
   put "a,s,d,f" into tVar
   put 0 into tCount
   breakpoint
   repeat with x = 1 to the number of items in tVar
      put comma & item 1 to x of tVar after tVar
      add 1 to tCount
   end repeat
   put cr & "Final x: " & x after msg
   put cr & "Count: " & tCount after msg
   put cr & cr & "Test 6" after msg
   put "a,s,d,f" into tVar
   put 0 into tCount
   breakpoint
    repeat for each item x in tVar
      put comma & item 1 to (tCount + 1) of tVar after tVar
      add 1 to tCount
   end repeat
   put cr & "Final x: " & x after msg
   put cr & "Count: " & tCount after msg
end mouseUp



More information about the Use-livecode mailing list