Delete the first entry of an array.

Peter TB Brett peter.brett at livecode.com
Fri Mar 25 04:03:31 EDT 2016


On 2016-03-25 08:33, Peter W A Wood wrote:
> I have an array which contains a second array. (myArray[“numbers”][]).
> The numbers array is a traditional array with elements 1..n. I want to
> remove the first element of the numbers array and shuffle all the
> others up. Is there a simple way of doing this in LiveCode (equivalent
> to the shift() function in JavaScript)?
>
> I looked in the dictionary and found delete <variable> but couldn't
> get it to work, perhaps because I am trying to do this in a function
> that takes the array as an argument.
>
> The essence of the function is:
>
> function shift @pList
>   local tItem
>   put pList[“numbers”][1] into tItem
>   delete pList[“numbers”] 1					## Is this the correct syntax?
>   return tItem
> end shift
>
> If I can get delete to remove the first element of the array, will it
> automatically change the index of the remaining entries?

Hi Peter,

LiveCode arrays are actually dictionaries (more like JavaScript objects 
than JavaScript arrays).  There aren't currently any push/pop syntax for 
LiveCode arrays.

I would recommend two things:

1) Arrange your algorithm so that you extract things one-by-one from the 
_end_ of the numbered array, rather than the start.  This is more 
efficient!  Then you would do something like:

    function PopEnd @xArray
       local tLength, tItem

       put the number of elements in xArray into tLength
       if tLength is 0 then return empty

       put xArray[tLength] into tItem

       delete xArray[tLength]

       return tItem
    end PopEnd

2) Alternatively, you can shift everything along, which will be slow:

    function PopStart @xArray
       local tLength, tItem, tKey

       put the number of elements in xArray into tLength
       if tLength is 0 then return empty

       put xArray[1] into tItem

       repeat with tKey = 2 to tLength
          put xArray[tKey] into xArray[tKey - 1]
       end repeat
       delete xArray[tLength]

       return tItem
    end PopStart

3) Another option, if you don't care about what order you get your 
values in, is to remove an item from anywhere, cheaply, by moving the 
last item in the list on top of the one you've removed:

    function RemoveAt @xArray, pIndex
       local tLength, tItem

       if pIndex is not among the keys of xArray then return empty
       put the number of elements in xArray into tLength

       put xArray[pIndex] into tItem
       put xArray[tLength] into xArray[pIndex]
       delete xArray[tLength]

       return tItem
    end RemoveAt


I hope that's helpful.

Would anyone be interested in a Feature Exchange for adding some new 
array operators (such as push and pop) to the language?

                                          Peter

-- 
Dr Peter Brett <peter.brett at livecode.com>
LiveCode Open Source Team

LiveCode 2016 Conference https://livecode.com/edinburgh-2016/




More information about the use-livecode mailing list