Map Manipulations

Map Manipulations

Table of Contentsup

 Overview

Maps provide a more general way of storing elements. Map collection type in Deluge allows you to store a pairs of elements, such as "keys" and "values", in which each key maps to one value. Conceptually, Lists are Maps with numeric keys.

The following sections explain the different types of map variables available in Deluge.

 Create Map

The CreateMap deluge syntax creates a new empty map expression. If the target map variable is already defined as map variable, then all its content will be erased. The map expression related tasks (put, putAll, remove, clear) and opertions can be done only if the map variable is defined.

map() - map constructor to create and initalize map variable.
{} - Holds map values. Handy syntax for creating map with some initial values.
Example:  hdrs = { "Email" : "support@mydomain.com", "Passwd" : "mypassword", "accountType" : "HOSTED_OR_GOOGLE", "service" : "cl" };

Example

In the following sample code, a map named map1 is defined in the on add -> on load block and initialized with the map() constructor.

map1 = map(); // map constructor to initalize the map variable named map1.
map1.put("creator","http://crm.zoho.com");
map1.put("writer","http://writer.zoho.com");
map1.put("sheet","http://sheet.zoho.com");                                       
              // Following is map constructor to declare a variable with default values
               hdrs = { "Email" : "support@mydomain.com", "Passwd" : "mypassword", "accountType" : "HOSTED_OR_GOOGLE", "service" : "cl" };

 Put Key

The PutKey Deluge task inserts a key-value pair into the map variable. If the specifed key is already present in the map-variable, then the key's associated value will be replaced with a new value.

Syntax

<map1>.put(<key>,<value>);

In the above syntax,

<map1> refers to the name of the map variable.
<key> refers to the key to be inserted to the target map variable.
<value> refers to the value to be associated with the given key in the target map variable.

Example

map1 = map();
map1.put("John", "India");
map1.put("David", "USA");

 Put All Key

The Put All Key Deluge task inserts all key-value pairs into the specified map variable. If the key(s) are already present in the map-variable, then the key's associated value will be replaced with new value.

Syntax

<target-map variable>.put(<source-map-variable>);

In the above syntax,

<target-map variable> is a map variable in which the action take place
<source-map-variable> is a map variable from where all the key-value mappings will be added to the above target map variable

Example

In the following sample code, the key-value pairs in the "Games" map is added to the "Sports" map.

{
Games=map();
Games.put("Cricket","Sachin");
Games.put("Tennis","SteffiGraf");
Games.put(("Badminton"),"SainaNehwal");
Sports=map();
Sports.put(Games);
}

 Remove Key

The Remove key Deluge task removes the specified key and its associated value from the target map variable.

Syntax

<map-variable>.remove(<key-name>);

In the above syntax,

<map-variable> refers to the map name from where the specified key and its associated value will be removed.
<key-name> refers to the key to be removed from the map variable

Example

In the following example, the key-value pair ("John", "India") available in map1 will be removed.

map1 = map();
map1.put("John", "India");
map1.put("David", "USA");
if(map1.containKey("John"))
{
      alert("John is lived in " + map1.get("John"));    
}
else
{
      alert("No Information available");
}
map1.remove("John");

 Clear Map

The Clear Map Deluge task removes all the key-value mappings from the map.

Syntax

<map>.clear();

In the above syntax,

<map> refers to the name of the map to be cleared.

Example

In the following example, all key-value mappings in "map1" will be removed.

map1 = map();
map1.put("John", "India");
map1.put("David", "USA");
alert(map1.size() + "entries available"); //before clear task, the map variable's size is 2
map1.clear();
alert(map1.size() + "entries available"); //after clear task, the map variable's size is 0

Share this post :

FacebookTwitter

Still can't find what you're looking for?

Write to us: support@zohodeveloper.com