The tables

The tables

> LeekScript Tutorial

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

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

Initialize an array

An array is represented by square brackets. The example below creates an empty array:

var foo = [];

We recognize the keyword var that we already use for variables. foo is therefore an array type variable.

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

Here we have initialized the array with 3 values. Basically, we put the chain "pear" in the first drawer, "banana" in the second and "apple" in the third. If we had reversed the first 2 fruits, we would have had a different picture.

It's nice to tidy up, but you have to know how to use it... Well, it's simple if you know how to count from 0 (because in computing, we like to count from zero!)

The first drawer bears the number 0, the second the number 1 and the third the number 2. If we had a table with 100 cells, the last would therefore bear the number 99. Okay, now what? It's simple, to use a box, just indicate its number between square brackets:

debug( fruit[1] );

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

What if we want to change the value? Nothing could be simpler, we do as for the variables, always specifying the box to modify:

fruit[1] = "cherry"; debug( fruit ); // will output ["pear", "cherry", "apple"]

Simple, right?

What if I ask for a box that does not exist? Kind in the previous example, I ask fruit[3]?

Well seen ! The third box is number 2, so box number 3 does not exist! In this case, the value ''null'' is returned.

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

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

Don't worry, young Padawan, you haven't broken anything at all! Since the boxes between the last element of the array (number 3) and the one you want to add (number 50) do not exist, the leekscript turned your array into an associative array. I'll explain that to you at the end of this tutorial. For the moment, consider that it is a normal table where there is a hole in the numbering of the cells.

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

Browse a table

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

Effectively. 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, there will be a shortage, and if there are less, there are extra lines! To find out how many squares an array has, the function is count.

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

Now, how to traverse the table without struggling? For example, if you want to know where the obstacles are on the map, getObstacles is for you... But the table has about a hundred boxes... not practical to do one by one! It is now that the Les Boucles reveal their full potential!

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

for (var k=0; k<nbObstacles; k++) { debug("box " + obstacles[k] + " is an obstacle."); }

For more precision, go back to the tutorial on The Loops.

So for the name of the enemies, the whole script would be

var enemies = getAliveEnemies(); var nbEnemies = count(enemies); for (var k=0; k<nbEnemies; k++) { var enemyName = getName( enemies[k] ); debug( enemyName + " is still alive."); }

Adding and removing elements

We have already seen how to add an element in an array but it is not practical because you have to constantly do appeal to the size of the array.

To make life easier, you can simply use the push function.

var fruit = ["pear", "banana", "apple"]; push(f