Could I access property lists by index?

For item list,there is a mechanism to access items of item list by index.But not found in property list.Is there a workaround?

Comments

  • EggplantMattEggplantMatt ForumAdmin admin
    Property lists are intended to have their values accessed by key. You can iterate over them, but you can't access them by index because the index isn't meaningful -- if you display a property list, its items will be listed alphabetically by key; there is no inherent order. The closest you can come is to do something like this:
    repeat with each item of keys(myplist)
    	put it & ":" & myplist.(it)
    end repeat
    
    You could create a list of one item property lists that could be accessed by index:
    put ((key1:"cat"), (key3:"bird"), (key4:"fish"), (key2:"dog")) into mylist
    repeat with each item of mylist
        put it.(the first item of keys(it))
    end repeat
    
    which produces the following output:
    cat
    bird
    fish
    dog
    
    You can create such a list with code like this:
    put "Ford" into "make" of myplist
    insert myplist nested after mylist
    put empty into myplist
    put "Edge" into "model" of myplist
    insert myplist nested after mylist
    put empty into myplist
    put "Blue" into "color" of myplist
    insert myplist nested after mylist
    put (item 2 of mylist).(the first item of keys(item 2 of mylist)) // outputs "Edge"
    
    You could make that last line a handler:
    function getIndexedProperty aList, index
        return (item index of aList).(the first item of keys(item index of aList))
    end getIndexedProperty
    
    So the last line of the previous code would become:
    put getIndexedProperty(mylist, 2)
    
  • Hi Matt,
    Thanks for your help.They look like perfect.
Sign In or Register to comment.