How do I add keys to or delete keys from an array?
When creating or working with arrays you will often want to add or delete keys from your array. This is simple in LiveCode, this lesson will show you how to create an array, add keys to it and delete keys from it.
An array is a variable that holds more than one value. Each of the values in the array is called an element and each element has a name, or key, which is used to access it.
There is more than one way to create an array in revTalk. You can use the split command to turn a list into an array or you can add elements individually, in this lesson we will add individual elements.
local tArray put "Miller" into tArray["Kevin"]
This line creates an element in the array with the key Kevin and the value Miller. The key is enclosed in square brackets and the value is put into the array element referenced by that key.
Now lets add some more elements to our array
put "Beaumont" into tArray["Benjamin"] put "Kenyon" into tArray["Oliver"] put "Buchanan" into tArray["Elanor"]
We can view the contents of our array in the variable watcher with the keys on the left (1) and values on the right (2)
Now, what if you want to completely remove an element from an array? That is the key and the value. You can do this using the delete variable command
delete variable tArray["Elanor"]
This deletes the element with the key "Elanor". Our array now looks like this.
Another thing you might want to do is to delete the value without deleting the key. You can do this by setting the value of the element to empty
put empty into tArray["Elanor"]
As you can see above the key still exists in the array but has no value.
You should now be able to create an array and add and remove elements from it as necessary.