Deleting an array element


#1

I’m sure I’m missing something simple, but how on Earth do you go about removing an element from a list?


#2

Currently you’re not missing anything, there is no way to remove a value from an array once set. You can set a blank value or track the count of “valid” array items, but once set the keys will remain indefinitely. There is a proposal for some special keywords to allow clearing an array and removing elements.

What is your specific scenario? Depending on what you need to accomplish there are a few ways to mitigate the problem with list variables or to avoid it with comma-separated string variables.


#3

Well, my current situation is needing to clean up an array that’s in a less-than-delightful state as a result of trying to figure out the right way to use a variable as an array index. I still haven’t gotten it perfected, but that’s another thread. So I’ve got an array of type decimal[] that looks like this:

{ "0":0, "1":0, "2":0, "3":0, "4":0, "5":0, "6":0, "7":55, "{$queueLength-1}":55, "{queueLength-1}":55, "queueLength-1":55 }

I tried just deleting it from the piston, the values come back with it when I re-create it. That seems like the kind of thing that’s either a bug or a brilliant UX feature, I just can’t figure out which it is. :stuck_out_tongue_winking_eye:


#4

Ah, here’s a trick for clearing an array: change the variable type temporarily to the simple string type. After saving the piston you can use the pencil icon in the Local variables section the edit the value – just clear what’s there and save the variable. You can then edit the piston to change it back to an array which will now be empty.

Not useful for emptying an array during normal operation of a piston, but great if you’ve done a lot of testing and have weird values in the array to clean up.