Re: Converting Associative List to Record
Re: Converting Associative List to Record
- Subject: Re: Converting Associative List to Record
- From: Nigel Garvey <email@hidden>
- Date: Fri, 7 Nov 2003 00:57:41 +0000
kai wrote on Thu, 06 Nov 2003 20:12:57 +0000:
>
on Thu, 6 Nov 2003 13:28:07 +0100, Frank <email@hidden> wrote:
>
>
> As a new Applescript user I was wondering if there is an easy way to
>
> create records from associative list:
>
on textFrom(v)
>
try
>
e of v
>
on error e
>
e's text 16 thru -2
>
end try
>
end textFrom
>
>
on recordFromList(l)
>
set c to count l
>
if c mod 2 > 0 then error "Can't convert odd number of items to record."
>
set r to {}
>
repeat with n from 1 to c by 2
>
set {k, v} to l's items n thru (n + 1)
>
set r to r & (run script "{" & k & ":" & textFrom(v) & "}")
>
end repeat
>
r
>
end recordFromList
>
>
set l to {"text", "hello", "integer", 3, "real", 12.8, "list", {"a", "b",
>
3}, "record", {a:1, b:2}}
>
>
recordFromList(l)
>
>
--> {text:"hello", integer:3, real:12.8, list:{"a", "b", 3}, record:{a:1,
>
b:2}}
An old trick that still seems to work in Jaguar is:
on recordFromList(assocList)
try
{<<class usrf>>:assocList}'s x
on error msg
run script text 16 thru -2 of msg
end try
end recordFromList
set l to {"text", "hello", "integer", 3, "real", 12.8, "list", {"a",
"b", 3}, "record", {a:1, b:2}}
recordFromList(l)
--> {|text|:"hello", |integer|:3, |real|:12.8, |list|:{"a", "b", 3},
|record|:{a:1, b:2}}
... though, as you can see, it doesn't encourage illegal user labels!
(<< and >> should be double-chevron characters, obtained on English
keyboards by pressing option-\ and shift-option-\ respectively.)
NG
_______________________________________________
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.