Map in script

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Rafi4
Posts: 281
Joined: 01 Dec 2017 05:23

Map in script

Post by Rafi4 » 15 Nov 2018 04:11

Hi
How can I add and remove value in map (script).
Give me one example.
Thanks from record4
No.1 Automation app in play store Automagic Premium
Samsung Galaxy j2 non rooted.
Android 5.1.1

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: Map in script

Post by digitalstone » 15 Nov 2018 09:24

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)
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
Rafi4
Posts: 281
Joined: 01 Dec 2017 05:23

Re: Map in script

Post by Rafi4 » 15 Nov 2018 11:27

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
No.1 Automation app in play store Automagic Premium
Samsung Galaxy j2 non rooted.
Android 5.1.1

User avatar
digitalstone
Posts: 342
Joined: 21 Oct 2017 12:36
Location: The Netherlands

Re: Map in script

Post by digitalstone » 15 Nov 2018 13:42

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)
Phone: LG Nexus 5X (rooted vanilla Android 7.1.2)

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: Map in script

Post by Desmanto » 15 Nov 2018 18:59

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
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply