I’m getting results back from a SOAP query that filters down to something like…
{{|key|:“Id”, value:“12345”}, {|key|:“TitleId”, value:“321”}, {|key|:“Code”, value:“060606”}, {|key|:“Description”, value:“CSDEMO”}, {|key|:“AnotherValue”, value:“210”}}
I can get element by Id number so
set myValue to the value of Item 1 of myResults will work
But that full SOAP call is pretty big and I’d like to do it by the name of the key pair returned. i.e.:
set myValue to the value of Item “Code” of myResults.
but so far no luck. Can’t help feeling I’ve missed something obvious…
Here ya go:
property someTable : {{|key|:"Id", value:"12345"}, {|key|:"TitleId", value:"321"}, {|key|:"Code", value:"060606"}, {|key|:"Description", value:"CSDEMO"}, {|key|:"AnotherValue", value:"210"}}
set myValue to getValueForKey(someTable, "Code")
on getValueForKey(theTable, givenKey)
set listItemsCount to count theTable
repeat with numIndex from 1 to listItemsCount
if |key| of item numIndex of theTable is givenKey then return value of item numIndex of theTable
end repeat
end getValueForKey
On 2nd thought - here is some code to create & maintain an associative list (with help from Nigel Garvey & DJ Bazzie Wazzie:
-- script 19-5 from Sanderson & Rosenthal
-- 0.1 removed a 'my' from find routine to make it work: http://macscripter.net/viewtopic.php?id=37436 post #3
-- 0.2 changed to camelCase
-- prefixed private property/handler names with underscore
-- 0.3 changed to use list index, not reference (Nigel Garvey, same thread)
-- 0.4 DJ's modification - uses an init handler (http://macscripter.net/viewtopic.php?id=37818)
--
-- Test code ------------------------
-- Create a new associative list object
set friendsAges to makeAssociativeList()'s init()
-- Store some values
setItem("Jan", 27) of friendsAges
setItem("Sam", 29) of friendsAges
setItem("Bob", 35) of friendsAges
-- Retrieve some values
display dialog "Jan is " & getItem("Jan") of friendsAges -- "Jan is 27"
display dialog "Bob is " & getItem("Bob") of friendsAges -- "Bob is 35"
--display dialog "Frank is " & getItem("Frank") of friendsAges -- Key not found error
-- delete item
deleteItem("Jan") of friendsAges
_listItems of friendsAges --> {{theKey:"Sam", theValue:29}, {theKey:"Bob", theValue:35}}
-- end Test code ------------------------
on makeAssociativeList()
script AssociativeList
property class : "associative list"
property _listItems : {}
on init()
return me
end init
on _findRecordForKey(givenKey)
set listItemsCount to (count my _listItems)
considering diacriticals, hyphens, punctuation and white space but ignoring case
repeat with numIndex from 1 to listItemsCount
if theKey of item numIndex of my _listItems is givenKey then return numIndex
end repeat
return missing value
end considering
end _findRecordForKey
on setItem(givenKey, givenValue)
set numIndex to _findRecordForKey(givenKey)
if numIndex is missing value then
set end of my _listItems to {theKey:givenKey, theValue:givenValue}
else
set theValue of item numIndex of my _listItems to givenValue
end if
return
end setItem
on getItem(givenKey)
set numIndex to _findRecordForKey(givenKey)
if numIndex is missing value then
error "The key wasn't found." number -1728 from givenKey
end if
return theValue of item numIndex of my _listItems
end getItem
on countItems()
return count my _listItems
end countItems
on deleteItem(givenKey)
set numIndex to _findRecordForKey(givenKey)
if numIndex is missing value then
error "The key wasn't found." number -1728 from givenKey
end if
set item numIndex of my _listItems to missing value
set _listItems to every record of my _listItems
return
end deleteItem
end script
end makeAssociativeList
That worked perfectly. Thanks for your help.