Bug or just something you can't do ?

Peter Haworth pete at lcsql.com
Mon Oct 1 19:52:04 EDT 2012

Hi Alex,
Not sure if this will reduce the complexity of dealing with
arrays/subarrays but when this came up before, I learned something about
array keys I didn't know before, posted by Dick Kriesel.  An extract from
his post follows.

LC has a way to get and set a node at any level of an array, without code
that concatenates [ ] to specify the node.  In an array reference a single
[ ] can enclose an array of keys, as you can see in the code below.  Put
the following into a button and click it.  The code invokes the debugger so
you can see the array and the result string on the variables tab.

on mouseUp
   local tArray, tString
   put 4 into tArray[ 1 ][ 2 ][ 3 ]
   put getNodeViaListOfKeys( tArray, ( 1, 2, 3 )) after tString -- that is,
get tArray[ 1 ][ 2 ][ 3 ]
   setNodeViaListOfKeys tArray, ( 5, 6, 7, 8 ), 9 -- that is, put 9 into
tArray[ 5 ][ 6 ][ 7 ][ 8 ]
   put comma & getNodeViaListOfKeys( tArray, ( 5, 6, 7, 8 )) after tString
-- 4,9
end mouseUp

function getNodeViaListOfKeys @pArray, pList
   local tKeys
   put pList into tKeys
   split tKeys by comma and null
   return getNodeViaArrayOfKeys( pArray, tKeys )
end getNodeViaListOfKeys

function getNodeViaArrayOfKeys @pArray, @pKeysArray
   return pArray[ pKeysArray ]
end getNodeViaArrayOfKeys

command setNodeViaListOfKeys @rArray, pList, pValue
   local tKeys
   put pList into tKeys
   split tKeys by comma and null
   setNodeViaArrayOfKeys rArray, tKeys, pValue
end setNodeViaListOfKeys

command setNodeViaArrayOfKeys @rArray, @pKeysArray, @pValue
   put pValue into rArray[ pKeysArray ]
end setNodeViaArrayOfKeys
lcSQL Software <http://www.lcsql.com>

On Mon, Oct 1, 2012 at 3:36 PM, Alex Tweedly <alex at tweedly.net> wrote:

> I can't see how to do that (without some *really* ugly code within the
> handler). The problem is that (in the handler) the parameter is an array
> passed in by reference so that it can be modified; sometimes I want to call
> it with an entire array, others with a sub-array (i.e. an array element
> which is itself an array). So the handler can't simply check whether what
> was passed was an array. It could require an extra parameter for the array
> index, and if that was empty use the array parameter as itself, and if
> non-empty it could index the array - but that means that every usage of the
> parameter within the handler becomes subject to an if-test.  Oh, btw, the
> real handler has more than one such parameter :-(

More information about the Use-livecode mailing list