Atkinson dither algorithm & 'for each' loop

Mark Waddingham mark at livecode.com
Thu Oct 12 13:51:09 EDT 2017


On 2017-10-12 19:35, Alejandro Tejada via use-livecode wrote:
> Peter Read wrote:
>> One note of caution regarding the use of the "repeat for each" loop,
>> whilst you will get a loop iteration for every value in the collection
>> (fldhexa3 in your example), you are not guaranteed the order in
>> which they will occur. This doesn't matter in a lot of cases but
>> does matter when the sequence is significant.  In the case of your
>> example I believe sequence is critical, otherwise the pixels might
>> appear to be scrambled!

Prior to 7, you had to be careful not to modify either the container 
being iterated over *or* the var holding the chunk for the current 
iteration as it could cause issues as described (actually, it was 
fundamentally unsafe to do that, as it could result in a crash). i.e.

   repeat for each item tItem in tContainer
     replace "Foo" with "B" in tContainer -- BAD - MIGHT CAUSE PROBLEMS
     put "a' into char 2 of tItem -- BAD - MIGHT CAUSE PROBLEMS
   end repeat

 From 7 onwards, this is no longer the case. You can do whatever you want 
with either the container. Indeed, in 7, the engine essentially operates 
as if it was iterating over a copy of the container and with a copy of 
the chunk (i.e. tItem and tContainer in the above snippet).

Upshot - you can do whatever you want with your variables - the engine 
ensures that what you do won't affect what you asked for to begin with 
(in particular, if you asked to iterate over tContainer, then it will 
iterate over tContainer as it was at the point you first asked - any 
changes to tContainer after the initial repeat for each line will have 
no effect on the repeat for each clause).

Warmest Regards,

Mark.

-- 
Mark Waddingham ~ mark at livecode.com ~ http://www.livecode.com/
LiveCode: Everyone can create apps




More information about the use-livecode mailing list