Arrays

An array is used to store multiple values in a single variable.

Example array:

browser = ["Safari", "IE", "FireFox", "Chrome"]

the example given above shows an array with 4 elements (values), the elements are "strings". Each element is seporated by commas: ","

Arrays are indexed from 0, meaning the first elements index is 0, the second elements is 1 and so on.

An array element can be referenced separately, like so:

browser = ["Safari", "IE", "FireFox", "Chrome"]
echo browser[0]

The above example will return "Safari" as safari has an index of 0 within the "browser" Array.

Array Functions

The following functions can be used to manipulate an array.

Click each function name in the table below for more details of it, with examples.

Function

Type

Description

push

int

Adds one or more elements to the end of an array and returns the new length of the array.

pop

string

Removes the last element from an array and returns the value of that element.

unshift

int

Adds one or more elements to the beginning of an array and returns the new length of the array. The other elements in the array are moved from their original position, i, to i+1.

shift

int

Removes the first element from an array and returns that element. The remaining array elements are moved from their original position, i, to i-1.

concat

array

Used to join two or more arrays together.

sort

array

Used to sort the order of array elements.

join

string

Takes an array and joins the elements into one string.

IndexOf

int

Checks for the first instance of an element and returns its position.

lastIndexOf

int

Checks for the last instance of an element and returns its position.

reverse

array

Reverses the order of the elements in the array.

splice

array

Add or remove elements to or from an array, and return the removed elements.

slice

array

Returns the selected elements in an array, as a new array object.

length

int

Returns the length of an array.

substr

String

This is used to return a section of a string

Other

Name

Type

Description

Multi Dimensional

array

Allows multiple arrays within a single array.

Array Collections

array

An array consisting of JSON objects.

push

Usage: array.push("x")

Adds one or more elements to the end of an array and returns the new length of the array.

Example:

Result:

pop

Usage: array.pop()

Removes the last element from an array and returns the value of that element.

Example:

Result:

unshift

Usage: array.unshift("x")

Adds one or more elements to the beginning of an array and returns the new length of the array. The other elements in the array are moved from their original position, i, to i+1.

Example:

Result:

shift

Usage: array.shift()

Removes the first element from an array and returns that element. The remaining array elements are moved from their original position, i, to i-1.

Example:

Result:

concat

Usage: array.concat()

used to join two or more arrays together.

Example:

boy = ["Luke", "Bob","Jason"]

girl = ["Emma", "Katie", "Chiara"]

uni = ["Jordan","alex","Jessie"]

names = uni.concat(girl,boy)

echo names

Result:

Jordan,alex,Jessie,Emma,Katie,Chiara,Luke,Bob,Jason

sort

Usage: array.sort()

Used to sort the order of array elements.

Example:

Result:

join

Usage: array.join()

Takes an array and joins the elements into one string.

Example:

Result:

IndexOf

Usage: array.indexOf()

Checks for the first instance of an element and returns its position.

If the item is not found, it will return as "-1"

If the item is present more than once, the indexOf method returns the position of the first occurrence.

Example:

array = ["google", "yahoo", "DuckDuckGo", "Bing"]

echo array.indexOf("DuckDuckGo")

Result:

lastIndexOf

Usage: array.lastIndexOf()

Checks for the last instance of an element and returns its position.

If the item is not found, it will return as "-1"

If the item is present more than once, the lastIndexOf method returns the position of the last occurrence.

Example:

array = ["google", "yahoo", "DuckDuckGo", "Bing","DuckDuckGo","google"]

echo array.lastIndexOf("DuckDuckGo")

Result:

reverse

Usage: array.reverse()

Reverses the order of the elements in the array.

Example:

count = ["1","2","3","4","5"]

echo count.reverse()

Result:

splice

Usage: array.splice()

adds or removes elements to or from an array, and returns the removed elements.

Example:

browser = ["Safari", "IE", "FireFox", "Chrome"]

echo browser.splice(2, 2)

Result:

slice

Usage: array.slice()

The slice() method returns the selected elements in an array, as a new array object.

The slice() method selects the elements starting at the given start argument, and ends at, but does not include, the given end argument.

Example:

browsers = ["IE", "Chrome", "FireFox", "Safari", "SeaMonkey"]

Good = browsers.slice(1, 3)

Result:

length

Usage: array.length

Returns the length of an array as an integer.

Example:

array = SearchEnemyCastles(10) echo "length: " + array.length + ", id: " + array[0].id + ", CityName: " + array[0].name + ", userName: " + array[0].userName + ", alliance: " + array[0].allianceName + ", prestige: " + array[0].prestige + ", honor: " + array[0].honor

Result:

Multi Dimensional

Usage: array = [1,["a1","a2","a3"],["b1","b2","b3"],2]

Example:

array = ["bob",["count",1,2,3],["letters","a","b","c"],"bill"]

echo array[1][0]+" The amount of "+array[2][0]"in this array."

echo array[1][1]+". "+array[1][1]

echo array[1][2]+". "+array[1][2]

echo array[1][3]+". "+array[1][3]

echo array[0]+" & "+array[3]+" have been added to the main array also."

Result:

Array Collections

This method allows you to define JSON objects within an array.

Usage: array = [ {a:1},{b:2} ]

Example:

array = [ {a:1},{b:2} ]

echo array[0].a

echo array[1].b

Result:

substr

Returns a select section of a string.

options are required in the substr brackets "substr(options)"

Usage: string.substr()

In the Example below the options are "0, 4" this will start the new string at char "0" of the existing string, and end it 4 chars after.

Example: theString = "NEAT Bot"

echo theString.substr(0, 4)

Result:


CategoryFunctions

Arrays (last edited 2015-04-17 00:19:57 by LKD70)