Dynamic scripted nested array keys?

Brian Milby brian at milby7.com
Sat Apr 24 09:26:03 EDT 2021


Easiest way is to use split...

Given a tree widget this will take a path from field 1 and put the value at
that path in field 2

on mouseUp pMouseButton
   local tTree, tLoc
   put the arrayData of widget 1 into tTree
   put field 1 into tLoc
   split tLoc by comma
   put tTree[tLoc] into field 2
end mouseUp

Thanks,
Brian

On Sat, Apr 24, 2021 at 9:06 AM Paul Dupuis via use-livecode <
use-livecode at lists.runrev.com> wrote:

> I have only partially been following this thread, but if you need to
> find the array 'content' in an arbitrary nested array from a comma
> delimited path of array keys, recursion is the way to do it:
>
> function fetchArrayContentFromPath pArray,pPath
>    -- pArray is an array
>    -- pPath is a comma delimited 'path' of array keys to the desired
> content
>    local tKey
>    if pArray is not an array then
>      return pArray -- I have reached the content, so return that content
>    else if pPath is empty then
>      return pArray -- should not happen in a tree widget, but this is if
> the path ends in an array instead of the content of an array element
>    else
>      put item 1 of pPath into tKey -- get the key of element in the
> currnet path
>      delete item 1 of pPath        -- remove it from the current path,
> so the path is the next set of key(s)
>      return fetchArrayContentFromPath(pArray[tKey],pPath) - recursively
> fetch the remainig keys from the currnet array element
>    end if
> end fetchArrayContentFromPath
>
> It will not matter is the 'path' is key1,key1 or
> key1,key2,key3,key4,key5,key6,key7, or whatever.
>
>
> On 4/24/2021 7:10 AM, Keith Clarke via use-livecode wrote:
> > 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
> >
> > _______________________________________________
> > use-livecode mailing list
> > use-livecode at lists.runrev.com
> > Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> > http://lists.runrev.com/mailman/listinfo/use-livecode
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list