Re: Newbie question: extracting records from lists
Re: Newbie question: extracting records from lists
- Subject: Re: Newbie question: extracting records from lists
- From: John Stewart <email@hidden>
- Date: Sat, 13 Sep 2003 16:58:13 -0400
On 9/13/03 at -0700 Luke Evans said this
>
I'm trying to have a list of records from which I can extract or add
>
unique records (with a name as the key).
>
Such a list could be created like this:
>
set mylist to {{name:"bob", number:1}, {name:"fred", number:2},
>
{name:"eric", number:3}}
>
>
Now, with Applescript's rich reference types, you'd be tempted to do
>
something like this to get a certain record:
>
get first item of mylist whose name is "fred"
>
(or somesuch), but name is not a property, so this is apparently not
>
available.
>
>
I'm assuming that I'm left having to write my 'extractor function'
>
longhand using a loop and comparator for the name field... or is there
>
yet some pithy reference that will extract the first occurrence of a
>
matching record for me?
>
>
Luke
This will work -
set str2rec to "{{aName:\"bob\", num:1}, {aName:\"fred\", num:2}, {aName:\"eric\", num:3}}"
set mylist to run script str2rec
name of item 3 of mylist
--> eric
However, using "name" and "number" as you have used them isn't a good idea since they are
AppleScript reserved words and may bite you when you least expect it though in this particular
instance they do work as you expected.
Personally I've never had a need to build record labels on the fly. Why not just set the record
structure up as a property of the script while you are writing it? Just fill them in as you need
them.
I.E.
property Rec : {aName:missing value, num:missing value}
set someNames to {"bob", "fred", "eric"}
set someNums to {1, 2, 3}
set mylist to {}
repeat with x from 1 to count someNames
copy Rec to end of mylist
copy item x of someNames to aName of item x of mylist
copy item x of someNums to num of item x of mylist
end repeat
return mylist
--> {{aName:"bob", num:1}, {aName:"fred", num:2}, {aName:"eric", num:3}}
John
_______________________________________________
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.