Arrays

Arrays

> LeekScript Tutorial

You probably know the analogy between variables in programming and boxes: the name of the variable is written on the box's label; when you use 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. Roughly speaking, that's all there is to it.

Initializing an array

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

var foo = [];

You'll recognize the var keyword, which we already use for variables. foo is therefore a variable of type array.

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

Here, we initialized the array with 3 values. Basically, we put 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.

Putting things away is all well and good, but you need to know how to use them... Well, it's simple if you know how to count from 0 (because in programming, we like counting from zero!)

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

debug( fruits[1] );

This example displays slot number 1 (so the second slot) of the array called fruits. Here, it displays the string "banana".

And what if I want to change the value? Nothing could be simpler: you do it just like with variables, always specifying which slot to change:

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

Simple, right?

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

Well spotted! The third slot has number 2, so slot 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 slots 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, think of it as a normal array with a gap in the numbering of its slots.

Careful: this is how LeekScript 1 to 3 behave. Since LeekScript 4, an array can no longer grow this way: writing to a slot that doesn't exist, even fruits[3] right after the last one, does nothing and shows an error in the logs. To add an element, use push (see below), and to number the slots freely, an associative array [:] (see the end of this tutorial).

Some Leek Wars functions give you an array directly. This is the case, for example, with the getAliveEnemies function.

Looping through an array

All very well. Now I know how to create arrays, but how do I, say, 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!

True. The first idea would be to write

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 left out, and if there are fewer, some lines are useless! To find out how many slots an array has, the function to use is count.

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

Now, how do you go through the array without a headache? For example, if you want to know where the obstacles are on the map, getObstacles is just what you need... But the array holds about a hundred cells... not practical to handle them one by one! This is where loops show their full potential!

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

for (var k=0; k

classic array associative array

Declaring an empty array

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

Declaring a hard-coded array

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 given position

insert(accessibleCells, aCell, position);

accessibleCells[aCell] = value;

Looping through the arrayLooping without knowing the index

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

Looping over the index, starting from the beginning of the array

for(var i=0; i=0; i--) { var cell = accessibleCells[i]; .... } Looping while getting both the index and the cell

for(var i:var cell in accessibleCells) { .... } for(var cell:var distance in accessibleCells) { .... } Modifying an elementaccessibleCells[position] = newValue;accessibleCells[aCell] = newValue; Removing an elementRemoves by position

remove(accessibleCells, position);

Removes the first matching value found in the array

removeElement(accessibleCells, aCell); Removes by key

removeKey(accessibleCells, cellToRemove);

Removes the first matching value found in the array (so call it several times to remove every occurrence of the value)

removeElement(accessibleCells, distance);