In LeekScript, there are "classic" arrays of type "Array", and associative arrays of type "Map".
This somewhat special kind of array solves a common problem: say I want to keep track of my leek's characteristics, such as strength, wisdom, resistance... So I made an array:
var myLeek = []; push(myLeek, getStrength()); push(myLeek, getWisdom()); push(myLeek, 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, you can, and that's called an associative array!
var myLeek = [:]; myLeek["strength"] = getStrength(); myLeek["wisdom"] = getWisdom(); myLeek["resistance"] = getResistance(); debug( myLeek ); // Displays something like ["strength" : 150, "wisdom" : 20, "resistance" : 95] debug( myLeek["strength"] ); // Displays 150
The explicit names (called keys) here are "strength", "wisdom" 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 loop through an associative array?
The following loop:
for (var k = 0; k myMap = [:];
In strict mode, this type is required: on a Map declared simply with var m = [:], insertions do not compile (type error), whereas they are accepted on a Map.
You can then loop through the map like this:
for (string myKey : integer myValue in myMap) { debug("For the key " + myKey + " the matching value is " + myValue); }
Now it's your turn 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.