Re: Working With Records
Re: Working With Records
- Subject: Re: Working With Records
- From: Paul Berkowitz <email@hidden>
- Date: Thu, 15 Jan 2004 09:17:27 -0800
On 1/15/04 8:19 AM, "Steven Valenti" <email@hidden> wrote:
>
Records never seemed to work the way I wanted them and for that reason
>
avoided them. I need to understand them now and try to make use of
>
them. The following example results in an error "Can't get RecordNumber
>
of {FirstRecord:400, SecondRecord:800}
>
>
set RecordNumber to "FirstRecord"
>
set TheRec to {FirstRecord:400, SecondRecord:800}
>
get RecordNumber of TheRec
>
>
I know that if I write "get FirstRecord of TheRec" will return 400 and
>
work correctly, however my case is that I wont know what record will be
>
needed and thus need to use the variable RecordNumber to get the
>
correct record needed.
>
>
Can anyone shed a glimmer of light for me?
Records do not contain ordered items, only labeled items. There's no such
thing as first, second, etc. The way a record is encoded, there's no
ordering. If you need an ordered list, use a list, not a record. (You can
convert the record to a list before you store it, and it will still be in
the order you've assembled it. But the labels will vanish - it will become
just an ordered list of the values.)
It sounds to me as if you really only know how to deal with lists, so
perhaps you should make it a list.
In any case, in your script above, you're defining RecordNumber as a string,
so naturally you can't get the string "FirstRecord" of the record. It's
definitely quite frustrating how difficult it is to deal with the labels
(keys) of AppleScript records. People have made various attempts to
implement their own hash table methods in AppleScript - you'll probably hear
about a few of them here. But you can probably do what you want by using
lists, or lists of lists, or two parallel lists of string label names and
values, instead of records.
Here's one method:
------------------------------
set keyList to {"FirstRecord", "SecondRecord"}
set valueList to {400, 800}
set theIndex to my CollectUniqueItemIndex(keyList, "FirstRecord")
set theValue to item theIndex of valueList
to CollectUniqueItemIndex(theList, theItem) -- the Item can be string,
number, constant, app object or list
local aListMember
repeat with i from 1 to (count theList)
set aListMember to item i of theList
if aListMember = theItem then
set theIndex to i
exit repeat
end if
end repeat
return theIndex
end CollectUniqueItemIndex
----------------------------------
If you're dealing with long lists (say 100 items or more), you can use an
embedded script object in the handler for much greater speed:
to CollectUniqueItemIndex(theList, theItem)
local aListMember
script listScript
property listRef : theList
end script
repeat with i from 1 to (count theList)
set aListMember to item i of listScript's listRef
if aListMember = theItem then
set theIndex to i
exit repeat
end if
end repeat
return theIndex
end CollectUniqueItemIndex
--
Paul Berkowitz
_______________________________________________
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.