Manipulating Arrays

Change Elements

To change an array element, you can just access it with its list number and assign it a new value:
	products = ["A", "B", "C"]
	products[2] = "D"
	

This changes the value of the element with the list number two (remember, arrays start counting at zero, so the element with the list number two is actually the third element.) As we changed "C" to "D", the array now contains "A", "B" and "D".

Add Elements

Now, suppose that we want to add a new element to the array. We can add a new element in the last position by just assigning a value to the next position. If it does not exist, it is added at the end:
	products = ["A", "B", "C"]
	products[3] = "D"
	

Now, the array has four elements: "A", "B", "C" and "D".

It is not necessary to know the array length to add an element at the end. In order to do it, the add ([]) operator or the extend method can be used.

Example, the following method is equivalent to the previous one:
	products = ["A", "B", "C"]
	extend products      // add at the end
	    using "D"
	

Delete Elements

Elements can be deleted from an array by using the delete operator:
	products = ["A", "B", "C"]
	delete products[0]   // delete first element
	
Now, the array has two elements "B", "C".

Find Elements

The 'in' operator can be used to check if an element is contained in an array. The following code checks whether "A" is contained in the array products:
	products = ["A", "B", "C"]
	if "A" in products then
	    display "'products' contains the element 'A'"
	end
	
Now, if you want to get the index of the first occurrence of an element:
	products = ["A", "B", "A", "C"]
	index = indexOf(products, "A")
	
	if index != -1 then
	    display "'A' is located at position : " + index 
	end
	
Last examples will show the index 0. Instead, if you want to find the last occurrence, 'lastIndexOf' can be used:
	products = ["A", "B", "A", "C"]
	index = lastIndexOf(products, "A")
	
	if index != -1 then
	    display "'A' is located at position : " + index 
	end