Arrays

Arrays

> LeekScript Tutorial

You surely know the analogy between computer variables and boxes: the name of the variable is on the box's label; when you call the variable, you simply look at what's inside the box.

Well, we can make a similar analogy between arrays and a chest of drawers. A chest of drawers is a piece of furniture that contains several drawers... an array is an entity that contains several variables. Schematically, that's all.

Initialising an array

An array is written with square brackets. The example below creates an empty array:

var foo = [];

You recognise the keyword var that we already use for variables. foo is therefore a variable of type array.

var fruits = ["pear", "banana", "apple"];

Here, we initialised the array with 3 values. Basically, we stored the string "pear" in the first drawer, "banana" in the second and "apple" in the third. If we had swapped the first 2 fruits, we would have had a different array.

It's all well and good to tidy things away, but you have to know how to use them... Well, it's simple if you can count from 0 (because in computing, we like to count from zero!)

The first drawer has number 0, the second number 1 and the third, number 2. If we had an array of 100 cells, the last one would have number 99. Ok, and now? It's simple: to use a cell, you just give its number in square brackets:

debug( fruits[1] );

This example will therefore display cell number 1 (the second cell, then) of the array called fruits. Here it will therefore display the string "banana".

And if we want to change the value? Nothing simpler, we do as for variables, always specifying the cell to modify:

fruits[1] = "cherry"; debug( fruits ); // will display ["pear", "cherry", "apple"]

Simple, isn't it?

And if I ask for a cell that doesn't exist? Like in the previous example, I ask for fruits[3]?

Well spotted! The third cell has number 2, so cell number 3 doesn't exist! In that case, the value ''null'' is returned.

Well, I tried your thing with the code below and it displays weird stuff, looks like I broke everything!

var fruits = ["pear", "banana", "apple"]; debug( fruits ); // will display ["pear", "banana", "apple"] fruits[3] = "pineapple"; debug( fruits ); // will display ["pear", "banana", "apple", "pineapple"] fruits[50] = "cherry"; debug( fruits ); // will display [0:"pear", 1:"banana", 2:"apple", 3:"pineapple", 50:"cherry"]

Don't worry, young Padawan, you didn't break anything at all! Since the cells between the last element of the array (number 3) and the one you're trying to add (number 50) don't exist, leekscript turned your array into an associative array. I'll explain that at the end of this tutorial. For now, consider it a normal array with a gap in the numbering of the cells.

Some leekwars functions give us an array directly. This is the case, for example, of the function getAliveEnemies.

Going through an array

All that's very well. Now, I know how to create arrays but how do I, for example, display the names of all the opponents? I don't know how many there are and I'm not going to do it all by hand!

Indeed. The first idea would be to do

var enemies = getAliveEnemies(); debug( getName(enemies[0]) ); debug( getName(enemies[1]) ); debug( getName(enemies[2]) ); debug( getName(enemies[3]) ); debug( getName(enemies[4]) );

But if there are more than 5 enemies, some will be missing, and if there are fewer, there are extra lines! To know how many cells an array has, the function is count.

var enemyCount = count(enemies); debug("there are " + enemyCount + " enemies still alive.");

Now, how do we go through the array without struggling? For example, if you want to know where the obstacles are on the map, getObstacles is made for you... But the array has about a hundred cells... not practical to do one by one! This is when loops reveal all their potential!

var obstacles = getObstacles(); var obstacleCount = count(obstacles);

for (var k=0; k

classic array associative array

Empty array declaration

var accessibleCells = []; var accessibleCells = [:];

Hard-coded array declaration

var accessibleCells = [252, 253, 254, 255, 270, 271, 272, 287, 288, 289, 290, 305, 306, 307, 322, 323, 324, 325, 340, 341, 342, 357, 358, 359, 360];

var accessibleCells = [252 : 3, 253 : 3, 254 : 3, 255 : 3, 270 : 2, 271 : 2, 272 : 2, 287 : 3, 288 : 1, 289 : 1, 290 : 3, 305 : 2, 306 : 0, 307 : 2, 322 : 3, 323 : 1, 324 : 1, 325 : 3, 340 : 2, 341 : 2, 342 : 2, 357 : 3, 358 : 3, 359 : 3, 360 : 3];

Adding valuesAdds at the end of the array

push(accessibleCells, aCell); Adds at the start of the array

unshift(accessibleCells, aCell);

Adds at a particular position

insert(accessibleCells, aCell, position);

accessibleCells[aCell] = value;

Going through the arrayGoing through it without knowing the index

for(var cell in accessibleCells) { .... }

Looping over the index from the start of the array

for(var i=0; i=0; i--) { var cell = accessibleCells[i]; .... } Going through it while getting the index and the cell at once

for(var i:var cell in accessibleCells) { .... } for(var cell:var distance in accessibleCells) { .... } Modifying an elementnot possible directly, you have to remove the old value then insert the new oneaccessibleCells[aCell] = newValue; Removing an elementYou must necessarily know the position of the element to remove

remove(accessibleCells, position); Removes by knowing the key

removeKey(accessibleCells, cellToRemove);

Removes the first value found in the array (call several times to remove all occurrences of the value, then)

removeElement(accessibleCells, distance);