Arrays and maps
Here is the code used in this lesson:
//ARRAYS
RuleContext arrays {
Rule rule {
// Array array
Array arString = [“hello”, “bye”, “other”]
Array arLocation = [Location(), Location()]
Array arBool = [true, false, true, false]
Integer a = arString.size() // 3
String b = arString.get(2) // "other"
arString.remove(1) // ["hello", "other"]
String c = arString.pop() // ["hello"] // c == "other"
Array arInt = arString.selected() // [] // it has no elements selected
arString.setSelected([0]) // when selected() is executed again, it will return [0]
Location loc = arLocation.shift() // [Location()] // it returns the removed element
arBool.reverse() // [false, true, false, true]
Array arInteger = [5, -1, 0, 9, 2]
arInteger.sort() // [-1, 0, 2, 5, 9]
arString.removeAll() // []
Array arInteger1 = arInteger.clone()
Bool c = (arInteger1 == arInteger) // false
Integer int = arInteger.indexOf(9) // 4
int = arInteger.indexOf(4) // -1
arString.add(“other”) // [“other”]
arInteger.set(3, -4) // [-1, 0, 2, -4, 5, 9]
arInteger.push(-1) // [-1, 0, 2, -4, 5, 9, -1]
Array arInteger9 = [1, 2, -3]
arInteger9.concat(arInteger) // [1, 2, -3, -1, 0, 2, -4, 5, 9, -1]
}
}
//MAPS
RuleContext maps {
Rule rule{
// Map map
Map mapString = {"a": "hello", "b": "bye", "c": "other"}
Map mapInteger = {"a": 1, "b": 2, "c": 3}
Integer a = mapString.size() // 3
String b = mapString.get("b") // "bye"
String c = mapString.get("c", "defaultValue") // "other"
c = mapString.get("d", "defaultValue") // "defaultValue"
mapString.remove("a") // {"b": "bye", "c": "other"}
mapString.removeAll() // {}
Map mapInteger1 = mapInteger.clone()
Bool d = mapInteger1 == mapInteger // false, different references
mapInteger.hasKey("a") // true
mapInteger.hasKey("other") // false
Array keys = mapInteger.keys() // ["a", "b", "c"]
Array values = mapInteger.values() // [1, 2, 3]
mapInteger.set("key1", 5) // {"a": 1, "b": 2, "c": 3, "key1": 5}
}
}