Try OpenEdge Now
skip to main content
BPM Events User's Guide
Persistent structures : Using persistent maps : Iterating over persistent map keys
 

Iterating over persistent map keys

The entries in a persistent map can be enumerated using the for each statement syntax.
foreach_statement() ::= 'foreach' ( iteratorDef() (',' iteratorDef())*)
            ['where' expr()] '{' statement()* '}'
iteratorDef() ::= <ID> [(':' | 'of') expr() ]
For example, the following persistent map is used for demonstrating iterations. The attributes of the keys are avg_resp_time, max_time, current_user which are of type float, integer and string respectively. UserCls is a persistent map.
fun printpmap(m: map<string, UserCls>){
    foreach e:m {
        val key = e.key;
        val value = e.value;
        pr("key: " + key + " :: value " + value);
    }
}

Initializing attributes of a persistent map instance

You can initialize the values of an attribute as shown in the following example.
val ucls0 = new UserCls ("ucls0"){
    "user_key01" : UserObject {avg_resp_time: 4.4, max_time:
8, current_user: "John"},
    "user_key02" : UserObject {avg_resp_time: 5.6, max_time:
4, current_user: "Smith"}
    };

Persistent map operations

This section explains various functions provided on persistent map.
To access a value associated with a particular key in the persistent map, get function is supported. The returned object is a part of the map. It does not create another object value. If this value is updated, then the map is also updated. For example,
ucls0.get("user_key01");
To update or put a new value into the persistent map, the put function is supported. A runtime exception is raised when trying to assign a nil value to a persistent map. For example,
ucls0.put("user_key01", object{avg_resp_time: 4.4, max_time: 8, current_user: "John"});
To remove a key, value pair from the persistent map, the remove function is provided. A runtime exception is raised when accessing a key that has been removed. For example,
ucls0.remove("user_key01");
To check if a particular key is present in the persistent map or not, the containsKey function is provided. If the key is present, then it returns a Boolean value of true. If the key is not present, then it returns a Boolean value of false. For example,
ucls0.containsKey("user_key01");
To get the number of key, value pairs in a persistent map, the size function is provided. It returns an integer value. For example,
ucls0.size();
To check if a persistent map is empty, the isEmpty function is provided. The isEmpty function returns a Boolean value of true if there is no key, value pairs in the persistent map. If there is at least one key, value pair, then it return false. For example,
ucls0.isEmpty();