Size: 8101
Comment:
|
← Revision 25 as of 2015-04-17 00:19:57 ⇥
Size: 8824
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 1: | Line 1: |
== 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 == |
|
Line 4: | Line 25: |
'''Functions''' ||<tablewidth="796px" tableheight="123px"#cccccc>'''Function ''' ||<#cccccc>'''Type''' ||<#cccccc>'''Description ''' ||||<style="text-align:center"> || || ||[[#push|push]] ||array ||Adds one or more elements to the end of an array and returns the new length of the array. || ||[[#pop|pop]] ||array ||Removes the last element from an array and returns the value of that element. || ||[[#unshift|unshift]] ||array ||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|shift]] ||array ||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. || |
||<tablewidth="796px" tableheight="350px"#cccccc>'''Function ''' ||<#cccccc>'''Type''' ||<#cccccc>'''Description ''' ||||<style="text-align:center"> || || ||[[#push|push]] ||int ||Adds one or more elements to the end of an array and returns the new length of the array. || ||[[#pop|pop]] ||string ||Removes the last element from an array and returns the value of that element. || ||[[#unshift|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|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. || |
Line 20: | Line 39: |
||[[#MultiDimensional|MultiDimensional]] ||array ||Allows multiple arrays within a single array. || ||[[#ArrayCollection|ArrayCollection]] || Array ||This allows multiple arrays, similar to !MultiDimentional but Sub-Arrays are also named|| ||[[#substr|substr]] || String ||This is used to return a section of a string|| |
||[[#substr|substr]] ||String ||This is used to return a section of a string || == Other == ||<tablewidth="796px" tableheight="75px"#cccccc>'''Name ''' ||<#cccccc>'''Type''' ||<#cccccc>'''Description ''' ||||<style="text-align:center"> || || ||[[#Multi Dimensional|Multi Dimensional]] ||array ||Allows multiple arrays within a single array. || ||[[#Array Collections|Array Collections]] ||array ||An array consisting of JSON objects. || |
Line 39: | Line 67: |
. a,b,c,d . a,b,c,d,e . 5 |
. myArray = a,b,c,d . myArray = a,b,c,d,e . returnVal = 5 |
Line 58: | Line 86: |
. a,b,c,d . a,b,c . d |
. myArray = a,b,c,d . myArray = a,b,c . returnVal = d |
Line 96: | Line 124: |
. a,b,c,d . b,c,d . a |
. myArray = a,b,c,d . myArray = b,c,d . returnVal = a |
Line 121: | Line 147: |
Line 127: | Line 152: |
Line 137: | Line 161: |
Line 143: | Line 166: |
Line 153: | Line 175: |
Line 158: | Line 178: |
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. |
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. |
Line 170: | Line 190: |
Line 174: | Line 193: |
Line 186: | Line 203: |
Line 193: | Line 209: |
Line 197: | Line 212: |
Line 210: | Line 224: |
Line 226: | Line 239: |
Line 232: | Line 244: |
Line 250: | Line 261: |
Line 258: | Line 268: |
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 |
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 |
Line 265: | Line 274: |
= MultiDimensional = |
= Multi Dimensional = |
Line 283: | Line 291: |
'''Result:''' |
'''Result:''' |
Line 291: | Line 299: |
= ArrayCollection = This allows multiple arrays, similar to !MultiDimentional but Sub-Arrays are also named. Where as !MultiDimentional works using array[0][0], this method allows named sub-arrays. |
= Array Collections = This method allows you to define [[JSON#JSON_Object|JSON objects|class=green]] within an array. |
Line 298: | Line 304: |
Line 307: | Line 312: |
'''Result:''' |
'''Result:''' |
Line 312: | Line 317: |
Line 314: | Line 318: |
Returns a select section of a string. |
Returns a select section of a string. |
Line 321: | Line 324: |
Line 324: | Line 326: |
'''Example:''' theString = "NEAT Bot" |
'''Example:''' theString = "NEAT Bot" |
Line 329: | Line 330: |
Line 333: | Line 333: |
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 |
|
|
|
int |
Adds one or more elements to the end of an array and returns the new length of the array. |
||||
string |
Removes the last element from an array and returns the value of that element. |
||||
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. |
||||
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. |
||||
array |
Used to join two or more arrays together. |
||||
array |
Used to sort the order of array elements. |
||||
string |
Takes an array and joins the elements into one string. |
||||
int |
Checks for the first instance of an element and returns its position. |
||||
int |
Checks for the last instance of an element and returns its position. |
||||
array |
Reverses the order of the elements in the array. |
||||
array |
Add or remove elements to or from an array, and return the removed elements. |
||||
array |
Returns the selected elements in an array, as a new array object. |
||||
int |
Returns the length of an array. |
||||
String |
This is used to return a section of a string |
Other
Name |
Type |
Description |
|
|
|
array |
Allows multiple arrays within a single array. |
||||
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:
- myArray = ["a","b","c","d"]
- echo "myArray = "+myArray
- returnVal = myArray.push("e")
- echo "myArray = "+myArray
- echo "returnVal = "+returnVal
Result:
- myArray = a,b,c,d
- myArray = a,b,c,d,e
- returnVal = 5
pop
Usage: array.pop()
Removes the last element from an array and returns the value of that element.
Example:
- myArray = ["a","b","c","d"]
- echo "myArray = "+myArray
- returnVal = myArray.pop()
- echo "myArray = "+myArray
- echo "returnVal = "+returnVal
Result:
- myArray = a,b,c,d
- myArray = a,b,c
- returnVal = d
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:
- myArray = ["a","b","c","d"]
- echo "myArray = "+myArray
- returnVal = myArray.unshift("_")
- echo "myArray = "+myArray
- echo "returnVal = "+returnVal
Result:
- a,b,c,d
- _,a,b,c,d
- 5
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:
- myArray = ["a","b","c","d"]
- echo "myArray = "+myArray
- returnVal = myArray.shift()
- echo "myArray = "+myArray
- echo "returnVal = "+returnVal
Result:
- myArray = a,b,c,d
- myArray = b,c,d
- returnVal = a
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:
- array = ["b", "a", "d", "c"]
- echo array.sort()
Result:
- a,b,c,d
join
Usage: array.join()
Takes an array and joins the elements into one string.
Example:
- array = ["a", "b", "c", "d"]
- echo array.join()
Result:
- "a,b,c,d"
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:
- 2
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:
- 4
reverse
Usage: array.reverse()
Reverses the order of the elements in the array.
Example:
count = ["1","2","3","4","5"]
echo count.reverse()
Result:
- 5,4,3,2,1
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:
FireFox,chrome
- Safari,IE
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:
Good = Chrome,FireFox
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:
length: 19, id: 17894, CityName: RED, userName: SomeN00b, alliance: 422, prestige: 6758, honor: 10495584936
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:
- - count The amount of letters in this array.
- - 1. a
- - 2. b
- - 3. c
- bob & bill have been added to the main array also.
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:
- - 1
- - 2
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:
- NEAT