Re: Appending a list to a list and getting one list.
Re: Appending a list to a list and getting one list.
- Subject: Re: Appending a list to a list and getting one list.
- From: has <email@hidden>
- Date: Tue, 14 May 2002 13:03:00 +0100
Paul Skinner wrote:
>
> From ASLG p.70-71
>
> You can use the concatenation operator (&) to merge or add values
>
> to lists.
[...]
>
repeat with n from 1 to numItems
>
copy n to the end of bigListRef
>
end repeat
I'm not entirely sure why the ASLG author felt the need to use a 'copy'
here instead of a 'set'. (Unless I've missed something?) The above is
functionally the same as:
repeat with n from 1 to numItems
copy n to n2
set the end of bigListRef to n2
end repeat
But given the circumstances, I don't see any point in doing more copy
operations than is necessary. I would simply use:
repeat with n from 1 to numItems
set the end of bigListRef to n
end repeat
>
repeat with n from 1 to numItems
>
set bigListRef to bigListRef & n --SuperSlowDowner!
>
end repeat
[...]
>
The example made me think that they are doing a similar thing
>
(concatenating) in the 10K iterations as they show prior to it. Not so.
'list1 & list2' is a concatenation. AS creates a new list by copying all of
the content from list1 to a new memory location, and copying all of the
content from list2 to the end of that.
'set end of theList to someValue' is not. It changes the existing theList
by appending someValue to it.
OK, in truth I don't really know how AS actually performs these two
operations internally. Perhaps there's some lookahead in the compiler
that's smart enough to say "I'm going to make sure this list has extra
memory space allocated to it so I've got some room to append extra items to
it without having to move home so often." Or maybe the list is able to grow
into any adjoining free memory during appends? Or perhaps AS adds a pointer
to the end of the existing memory block that points to a second block where
the list continues?
If anyone can actually tell me, I'd be tremendously grateful as this sort
of internal implementation information is a huge help in understanding how
to design efficient algorithms in AS. (e.g. Discovering how to write
efficient string manipulation algorithms that avoid excessive concatenation
was the first thing I learned from the wonderful folks on this list. I can
write functions now that are 10-20 times faster than the equivalent
functions I was writing a year ago - and I hope I've since gone some way to
repaying these original debts.)
>
Can anyone tell me how to append a list to a list and get just one
>
list?
Concatenation. Or, as Lachlan suggests, appending the items from the second
list to the first one at a time.
HTH
has
--
http://www.barple.connectfree.co.uk/ -- The Little Page of Beta AppleScripts
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.