Dynamic scripted nested array keys?
Keith Clarke
keith.clarke at me.com
Sat Apr 24 07:10:19 EDT 2021
Hi folks,
Inspired by Jaque’s (working - thank you!) response to my question “Show Tree widget row contents on hover” I’ve isolated the final piece of that puzzle that leaves me baffled.
Specifically, can LiveCode accept a dynamically built key for a nested multi-dimensional array? Copy the button script recipe below to see the problem.
Perhaps the LC array experts here can help explain why Jaque’s hard-wired key definition works but my attempts to replicate this syntax via scripting to support variable depth of arrays get ignored? Is it my syntax or just the way arrays work in LC…?
on mouseUp pButtonNumber
# Create a nested array
put "Content" into tArray["key1"]["key2"]["key3"]["key4"]
# Replicate the path response from the Tree widget actionInspect function
put "key1,key2,key3,key4" into pPath
# Jaque's 'hard-wired' array key recipe works!
answer tArray[item 1 of pPath][item 2 of pPath][item 3 of pPath][item 4 of pPath] --returns 'Content'
# Replicate Jaque's syntax dynamically
put the number of items in pPath into tKeyCount
put empty into iNum
repeat for each item i in pPath
add 1 to iNum
# Try building the full key string
put "[" & item iNum of pPath & "]"after tKey1
# Try building keys string to 'embed' into the regular array key syntax
put item iNum of pPath after tKey2
if iNum < tKeyCount then put "][" after tKey2
# Try building by each key
if i is not empty then put "[" & item iNum of pPath & "]" after tArray2
end repeat
# Dynamic tKey1 results
answer tKey1 -- returns '["key1"]["key2"]["key3"]["key4"]' ...seems 'correct'
answer tArray & tKey1 -- returns '["key1"]["key2"]["key3"]["key4"]' ...dynamic key definition is not appended to array(?)
# Dynamic tKey2 results
answer tKey2 -- returns '"key1"]["key2"]["key3"]["key4"'
answer tArray[tKey2] -- returns null ...dynamic key definition is not understood (as first level key)?
answer tArray2 -- returns '["key1"]["key2"]["key3"]["key4"]' ...dynamic key definition is not appended to array(?)
end mouseUp
Best,
Keith
More information about the use-livecode
mailing list