Array methods

Arrays include properties and methods to manage their elements.

Properties#

PropertyDescription
lenNumber of elements in the array
list = [10 20 30]

print list.len # 3

Methods#

MethodDescription
push(value)Adds an element to the end
pop()Removes and returns the last element
first()Returns the first element
last()Returns the last element
get(i)Returns the element at index i
set(i value)Replaces the value at index i
clear()Removes all elements
contains(value)true if the value exists in the array

Examples#

Modify elements#

list = ["Apple" "Orange"]

list.push "Banana"
print list.last()  # Banana

last = list.pop()
print last        # Banana

list.set 0 "Mango"
print list.get(0)  # Mango

Check state#

list = ["Apple" "Orange"]

print list.len       # 2

if list.contains("Apple")
    print "Contains Apple"

list.clear
print list.len  # 0