Re: Building applescript records with lists, accessing applescript records with strings
Re: Building applescript records with lists, accessing applescript records with strings
- Subject: Re: Building applescript records with lists, accessing applescript records with strings
- From: Emmanuel <email@hidden>
- Date: Fri, 4 Feb 2005 11:23:46 +0100
At 5:34 PM +1100 2/4/05, Malcolm Fitzgerald wrote:
set theKeys to {"a","b","c","d"}
set theValues to {97,98,99,100}
GetHashValue(item 2 of theKeys, BuildHash(theKeys,theValues))
-- 98
(*
-- build some data
set theKey to "a"
set theValue to "ha ha"
set theValueList to {}
set theKeyList to {}
repeat with i from 97 to 122
set end of theValueList to i
set end of theKeyList to ASCII character i
end repeat
*)
GetItem(theKey, theKeyList, theValueList)
--> 97
SetItem(theKey, theValue, theKeyList, theValueList)
--> {"ha ha", 98, 99, 100, ... }
GetItem(theKey, theKeyList, theValueList)
--> "ha ha"
on GetItem(theKey, theKeyList, theValueList)
set my text item delimiters to ASCII character 8
set delimKey to {"", theKey, ""} as string
set delimKeyList to {"", theKeyList, ""} as string
set n to offset of delimKey in delimKeyList
return item ((count (text items of (text 1 thru n of
delimKeyList))) - 1) of theValueList
end GetItem
on SetItem(theKey, theValue, theKeyList, theValueList)
set my text item delimiters to ASCII character 8
set delimKey to {"", theKey, ""} as string
set delimKeyList to {"", theKeyList, ""} as string
set n to offset of delimKey in delimKeyList
set item ((count (text items of (text 1 thru n of
delimKeyList))) - 1) of theValueList to theValue
return theValueList
end SetItem
You write a shorter (and faster) script if you use the p-list Suite in XMLLib:
-- build some data
set theplist to PlistNew
repeat with i from 97 to 122
PlistNewChild i at theplist key (ASCII character i)
end repeat
set theKey to "a"
set theValue to "ha ha"
GetItem(theKey, theplist)
--> 97
SetItem(theKey, theValue, theplist)
-->
GetItem(theKey, theplist)
--> "ha ha"
on GetItem(theKey, theplist)
PlistGet (PlistChild theplist key theKey)
end GetItem
on SetItem(theKey, theValue, theplist) -- sets, or creates
try
PlistSet theplist key theKey to theValue
on error
PlistNewChild theValue at theplist key theKey
end try
end SetItem
Emmanuel
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden