Re: FW: Making a list of lists
Re: FW: Making a list of lists
- Subject: Re: FW: Making a list of lists
- From: Axel Luttgens <email@hidden>
- Date: Thu, 02 Jan 2003 14:46:03 +0100
Jerry Podhajsky wrote:
>Sorry...forgot the subject.
>
> -----Original Message-----
> From: Jerry Podhajsky
> Sent: Wed 1/1/2003 1:48 PM
> To: email@hidden
> Cc:
> Subject: (no subject)
>
>
>
> I'm sure this is really simple, but I can't seem to find
>any good
> reference on it. I want to make a list of lists:
>
> set d1 to {date "9/8/20", 1}
> set d2 to {date "1/1/02", 2}
> set d3 to {date "12/1/02", 3}
>
> set masterlist to {}
>
> set masterList to d1 & d2 & d3 -- yields a string
>concatenation of all
> the list items.
>
> The result I want is:
>
> {{date "9/8/20", 1},{date "1/1/02", 2},{date "12/1/02",
>3}}
>
> All help appreciated.
>
> jp
>
To understand the seemingly negative result you obtained, as well as
some replies you already got, you have to go back to the definition of
the concatenation operator.
Its behavior is rather convoluted, but this is what makes its richness.
Here follows an attempt for such a definition [1].
Upon evaluation of:
expr1 & expr2
where expr1 and expr2 are two expressions,
if expr1 evaluates to a string
if expr2 evaluates a string
the result is the string composed of the
characters of expr1's value followed by the
characters of expr2's value
else if expr2'value can be coerced to a string
the result is the string composed of the
characters of expr1's value followed by the
characters of expr2's coerced value
else
it is an error
else if expr1 evaluates to a record
if expr2 evaluates to a record
the result is a record that begins with the
properties of expr1's value followed by the
properties of expr2's value (and if expr1 and
expr2 contain a property with the same name,
only the value of the property from expr1
appears in the result)
else
it is an error
else
(expr1 and expr2 both evaluate to lists or to
values that can be coerced to lists [2])
the result is a list containing the items in
expr1's value (possibly coerced to a list) followed
by the items in expr2's value (possibly coerced to
a list)
So, with your data, the evaluation of
d1 & d2 & d3
may be seen as two consecutive applications of the last part of the
definition.
You thus get a list composed of the items in d1 followed by the items in
d2 followed by the items in d3:
{date "9/8/20", 1, date "1/1/02", 2, date "12/1/02", 3}
Let's then write:
{d1} & {d2} & {d3}
Here we are concatenating three single item lists, and the same last
part of the definition applies. The result is thus a list composed of
the (unique) item of the first list followed by the (unique) item of the
second list followed by the (unique) item of the third list:
{{date "9/8/20", 1}, {date "1/1/02", 2}, {date "12/1/02", 3}}
That is, the result you wanted.
This allows another approach of your question.
Writing {d1} as we did above is just the same as writing, say, {1}; the
difference is that we are constructing a list from an expression instead
of a constant.
This may be generalized to multi-items lists, and one could just write
{d1, d2, d3}
instead of using the concatenation operator.
Now, if you had to construct your "masterlist" without knowing its
length in advance, you would have to append or prepend each item in turn
(possibly in a loop), either by using the concatenation operator, or by
using one of these commands:
"set end of ... to ..."
"copy ... to the end of..." commands.
"set beginning of ... to ..."
"copy ... to the beginning of..." commands.
HTH,
Axel
[1] Adapted from the "AppleScript Language Guide" (ASLG), which you may
find at:
<
http://developer.apple.com/techpubs/macosx/Carbon/pdf/AppleScriptLanguageGuide.pdf>
or somewhere in the documentation if you've installed the Developer Tools.
[2] Coercion to a list includes promotion to a single item list.
This is why one has:
1 & "a"
--> {1, "a"}
_______________________________________________
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.