Re: string as key to record
Re: string as key to record
- Subject: Re: string as key to record
- From: has <email@hidden>
- Date: Fri, 20 Sep 2002 18:38:14 +0100
Malcolm Fitzgerald wrote:
>
>>Is it possible to use a string as a key to a record?
>
>>Is there a coercion that would work here?
[...]
>
John's script does not work if varVal is a string. The script sees an
>
unquoted string value, expects that to be a variable and complains
>
that the variable is undefined. Including the quotes in the
>
concatenation fixes it.
>
>
set varKey to "a"
>
set varVal to "3"
>
set varRecord to run script "
>
{" & varKey & ":" & "\"" & varVal & "\"" & "}"
>
return a of varRecord
Life can be made much simpler if you avoid including the value here at all.
Try:
======================================================================
set varKey to "a"
set varVal to "3"
set recordConstructor to run script "
script
on newRecord(varVal)
return {" & varKey & " : varVal}
end newRecord
end script"
return recordConstructor's newRecord(varVal)
======================================================================
To get a value out again using a string as the key:
======================================================================
on getStringFromRecord(theRec, varKey)
set recordDeconstructor to run script "
script
on extractVal(theRec)
return " & varKey & " of theRec
end extractVal
end script"
return recordDeconstructor's extractVal(theRec)
end getStringFromRecord
getStringFromRecord({a:3, b:4}, "a")
======================================================================
(None of which does anything to check that the 'key' is a suitable value
and enclose it in bars/error if not, what to do if a key isn't found, etc.;
these issues also need to be addressed for any serious solution.)
BTW, I wrote a library, hashLib, for manipulating records this way way back
when (you can find it on ScriptBuilders) - if you really want to do this
sort of stuff then I'd suggest using that [by default it needs Smile, but
can easily be converted for vanilla use]. Olof Hellman has also done work
in this area, and there's a 3rd-party osax, RecordAccess, that will also do
the job if you're running OS8/9.
This all said, however, if you need to store values using strings as keys
then don't use records to begin with; they were never intended for this
task, and the above solutions are thoroughly kludgy and rather slow. It's
faster and cleaner to use a data structure designed specifically for this
task, such as an associative array [a.k.a. hash array]. AS doesn't have
associative arrays as a built-in type, unfortunately, but you can use a
3rd-party solution such as my associativeArrayLib library instead [see my
site].
HTH
has
--
http://www.barple.pwp.blueyonder.co.uk -- The Little Page of AppleScripts
_______________________________________________
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.