Page 1 of 1

Map in script

Posted: 15 Nov 2018 04:11
by Rafi4
Hi
How can I add and remove value in map (script).
Give me one example.
Thanks from record4

Re: Map in script

Posted: 15 Nov 2018 09:24
by digitalstone
Just removing the value itself and letting the variable intact

Code: Select all

variable=""
Removing the variable entirely would be

Code: Select all

removeVariable(variablename)

Re: Map in script

Posted: 15 Nov 2018 11:27
by Rafi4
Hi digitalstone
Map means functions in script "new map, new map from values, is empty, copy map, length,add map entry, remove map entry etc".
I am in bit confusion about these functions.
Thanks from record4

Re: Map in script

Posted: 15 Nov 2018 13:42
by digitalstone
Well, what do you want to do exactly? I may be wrong, but i get the idea you want to memorize every single function. This will be inefficient.
It all depends on what you're making. Then you decide what type of step/function you need for each obstacle you face, and then you search for that particular function. And over time you will learn those functions as a positive side-effect.

But for now, just check and read the description beneath the functionname within the script-help page.

I'll explain the first 2 since i don't have much time right now, but you'll get the general idea:
"new map" is to create a new map variable

Code: Select all

rafi_map = newMap()
"is empty" is to check if the map variable is without any value (true or false)

Code: Select all

isEmpty(rafi_map)

Re: Map in script

Posted: 15 Nov 2018 18:59
by Desmanto
To create a map object, you can use newMap() for blank map, or newMapFromValues(key, value) to define the map with keys and values directly.

Code: Select all

a = newMap();
b = newMapFromValues("apple", 11);
To add the map entry, you can use addMapEntry() or immediately assign the map keys to it.

Code: Select all

addMapEntry(b, "banana", 22); //add a new key "banana" with value 22
a["state"] = "on"; //add a new key "state" with value "on"
To remove an entry from the map, use removeMapEntry.

Code: Select all

removeMapEntry(b, "apple"); //remove key "apple" along with its value 11
length() and isEmpty() should be quite clear already, to check the length and to check if it is empty (no entry).

While copyMap() is used to flat copy. The difference with assigning directly is the copyMap() create new object instead of assigning it to the same pointer. So changes to existing map object won't change the new copied map.

Code: Select all

c = newMapFromValues("state", "on"); //create new Map c with key-value ("state", "on")
d = c; //assign d to point to object c, so both c and d point to map with key-value ("state", "on")
e = copyMap(c); //create a new map copy of c, e is separate map object
removeMapEntry(c, "state");
after removal, c and d now are empty, even though you only remove c. but e retain the key-value ("state", "on"), since it is new copy