7/23/08

VI: Arrays

Arrays is a variable that is divided into many sections and this sections can be called separately and is represented by <Variable Name>[<index>] the best advantage of arrays versus ordinary variables is indexes can be called with a variable or function, therefore making it easier to put condition or make many variables.

Declaring an array:


Before you can call an array value, you should declare it first or else it will not work
/run x={}


Placing a value in the array:


To insert a data on the array right away at declaration, just make:
/run x={<value1>,<value2>,<value3>}

now, variable x[1] will be equal to <value1>, x[2] equal to <value2>. x[3] is equal to <value1>
Note: you can add more or less than 3 values

To insert a data on an index:

/run x={} x[1]="hello"

Note: you cannot call the array variable alone eg. SendChatMessage(x)

To call an indexed value:

/run x={} x[1]="hello" SendChatMessage(x[1])

This is same as:
/run x={"hello"} SendChatMessage(x[1])

To call an indexed value using a variable on index:

/run x={} x[1]="hello" y=1 SendChatMessage(x[y])


To get the number of values in the array(how many is indexed)

/run x={} x[1]="hello" x[2]="hi" message("array x has"..table.getn(x).." Values")

Note: One of the most useful usage of array is using them on loops.



No comments: