Reverse a list

Alex Tweedly alex at tweedly.net
Mon Feb 16 18:08:58 EST 2015


On 16/02/2015 16:06, Bob Sneidar wrote:
> The For Each form is also quite handy as it eliminates the need for stuffing some variables in the process. As mentioned in past threads, the one downside is that you *MUST* not change the contents of the source data (and I think the each variable as well) as doing so will corrupt what ends up in the each variable.
>
> The reason for this (and the reason this form works so fast) is that LC takes the actual contents of what you are parsing, and goes through it one time, creating memory pointers at the delimiters. Other repeat forms will have to search through the variable on each iteration and find the next delimit point. Find line 1, find line 2, etc.
That's not quite correct. It doesn't do a single initial complete scan 
of the whole variable and keep all the pointers. What it does is (more 
like) keep track of how far it has currently processed, and then when it 
needs the next line, it scans from that remembered position forward 
until the next CR - and then remembers that as the current position.  
(Oh, and btw, it does remember how many chars were in the initial value, 
and uses that).

The fact that it does incremental scan rather than one initial scan can 
be seen from the following (totally useless :-) bit of code :

on mouseup
    put "abc" & CR & "def" & CR & "ghi" & CR into t
    repeat for each line L in t
       put the number of chars in L && L &CR after msg
       if L = "abc" then
          put "x" into line 2 of t
          put "qwert" after line 3 of t
       end if
    end repeat
end mouseup

An initial scan would have produced  :
3 abc
3 x
g
3 ijq

But we actually get:
3 abc
1 x
6 ghiqwe

It is significant that it doesn't do a complete scan, because that would 
be a cost penalty at the start of the loop, and so could make it 
unnecessarily expensive to do some operations. Again that can be seen 
from the following (take out of context) snippet of not quite real code ....

put "millions and millions of lines of data" into tVar
repeat for each line L in tvar
    if some condition then exit repeat
end repeat

If there were an initial scan, then you would do that whole costly scan 
even though you could actually exit the loop very early; because it's an 
incremental scan, you don't need to worry about that (so long as you are 
sure you will exit soon).

-- Alex.
> Since the data you are parsing has an index of memory pointers, you can imagine the hell that would ensue if all of a sudden you change the contents of that data. The pointers will no longer be valid, and in fact it is highly likely that LC will alter the memory location of the parsed data, so that your pointers will now point at random noise.
>
> Bob S
>
>
> On Feb 14, 2015, at 15:00 , Richard Gaskin <ambassador at fourthworld.com<mailto:ambassador at fourthworld.com>> wrote:
>
> The Dictionary entry for "repeat" notes that the "for each" form is much faster than "with".
>
>
>> and how come this is the first time I remember EVER hearing about this
>> difference?
> Good question.  This comes up in the forums and/or this list almost every month or so.
>
> The speed difference will vary according to the size of each line and the size of the lines, but "order of magnitude" is usually a pretty fair minimal expectation for the speed boost with this.
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list