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 revTalk, this lesson will show you how to create an array, add keys to it and delete keys from it.

What is an array?

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.

Create an array

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"]

View the array in the variable watcher

View the array in the variable watcher

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)

Deleting an element from an array

Deleting an element from an array

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.

Remove contents of an element without deleting the key

Remove contents of an element without deleting the key

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.

1 Comments

Jason

If I wanted to add a key to an array but have the user enter the information for both the key and the element from a text entry box how would I got about coding that? Do I do it in the text entry box, or the card? and can I have a button reveal the text entry box when clicked?

Add your comment

E-Mail me when someone replies to this comment