Re: can I make a list of records?
Re: can I make a list of records?
- Subject: Re: can I make a list of records?
- From: Arthur J Knapp <email@hidden>
- Date: Mon, 28 Jan 2002 14:40:13 -0500
>
Date: Mon, 28 Jan 2002 11:28:14 -0500
>
From: James Sentman <email@hidden>
>
Subject: can I make a list of records?
>
I'd like to be able to represent some data structures as lists of
>
records so that I can do things like
>
>
set myFirstRecord to {name:"jack", age:"32"}
>
set mySecondRecord to {name:"peter", age:"41"}
>
>
and then put them into a single list, but make it a list of records,
>
not just a list of strings which is what I always seem to get.
The "&" operator acts differently depending on the operands it
is working with.
The leftmost operand is the key to understanding what will
occur:
set v1 to { name:"jack", age:"32", uniqueLabel: 123 }
set v2 to { name:"peter", age:"41", specialLabel: 456 }
set v1v2 to v1 & v2
--
--> { name:"jack", age:"32", uniqueLabel: 123, specialLabel: 456 }
set v1v2 to v2 & v1
--
--> { name:"peter", age:"41", specialLabel: 456, uniqueLabel: 123 }
Where the two records share a label, the left record's labels are
used. Every unique label becomes a part of the new record.
Then there is string-concatenation:
set s1 to "Hello "
set s2 to "World"
set s1s2 to s1 & s2 --> "Hello World"
If the leftmost operand is a string, then the "&" operator
tries to coerce the rightmost operator to be a string:
set s1 to "Hello "
set num to 3.14159
set s1num to s1 & num --> "Hello 3.14159"
set lst to {1, 2, 3}
set s1lst to s1 & lst --> "Hello 123"
If the leftmost operator is neither a string or a record,
then AppleScript performs list-concatenation:
3.14159 & "Dude" & {"What", "the", "..."} & true
--
--> {3.14159, "Dude", "What", "the", "...", true}
To ensure that the list becomes a sublist, you must enclose
it in it's own list:
3.14159 & "Dude" & { {"What", "the", "..."} } & true
--
--> {3.14159, "Dude", {"What", "the", "..."}, true}
You can do the same with records:
set myFirstRecord to {name:"jack", age:"32"}
set mySecondRecord to {name:"peter", age:"41"}
set recordList to {myFirstRecord} & {mySecondRecord}
However, it is actually much better to use list-appending:
set myRecords to {}
set myRecords's end to myFirstRecord
set myRecords's end to mySecondRecord
{ Arthur J. Knapp, of <
http://www.STELLARViSIONs.com>
<
mailto:email@hidden>
try
<
http://www.seanet.com/~jonpugh/>
on error number -128
end try
}