In LeekScript, there are "classic" arrays of type "Array", and associative arrays of type "Map".
This somewhat special kind of array answers a specific need: suppose I want to keep track of my leek's characteristics, such as strength, wisdom, resistance, ... So I made an array:
var monPoireau = []; push(monPoireau, getStrength()); push(monPoireau, getWisdom()); push(monPoireau, getResistance());
My problem now is that I have to remember that strength is in the 1st slot, wisdom in the 2nd and resistance in the 3rd... Right now that's fine, but in 2 weeks I'll have completely forgotten!
The solution: instead of numbering the slots, it would be smarter to give them an explicit name. Well, that's possible, and it's called associative arrays!
var monPoireau = [:]; monPoireau["force"] = getStrength(); monPoireau["sagesse"] = getWisdom(); monPoireau["resistance"] = getResistance(); debug( monPoireau ); // Will display something like ["force" : 150, "sagesse" : 20, "resistance" : 95] debug( monPoireau["force"] ); // Will display 150
The explicit names (called keys) here are "force", "sagesse" and "resistance". This example shows how to declare them and how to use them. Keys can be integers or strings.
Handy! But then, how do you iterate over an associative array?
The following loop:
for (var k = 0; k maMap = [:];
In strict mode, this typing is essential: on a Map declared simply with var m = [:], insertions do not compile (type error), whereas they are accepted on a Map.
You can then iterate over the map like this:
for (string maCle : integer maValeur in maMap) { debug("Pour la clé " + maCle + " la valeur qui correspond est " + maValeur); }
Now it's up to you to use them. You'll see, once you know how to use them, you can't do without them!
Impossible de charger les données du jeu.
Vérifiez votre connexion et réessayez.