Operator Type:
Operator Scope of Action:
Operator Purpose:
Operator First Added:
Operator Last Altered:
Function [other Function type actions]
List [operators of similar scope]
Dictionary, Set & List operations [other Dictionary, Set & List operations operators]
Baseline
As at baseline
List.lookup(keyStr)
The command .lookup() It is intended for look-up tables (i.e. single dimension arrays). For the key keyStr value is supplied, the matched key's value is return. We can set up an example listing, using a List-type attribute:
$MyList = [ant:Wood ant;bee:Carder bee;cow:Jersey;dog:Labrador];
This creates a 4 item look-up list. The first list item has two parts - the key 'ant' and its paired value 'Wood ant'. Passing a key via .lookup, returns its key:
$MyString = $MyList.lookup("cow");
→ "Jersey", as list item 3's key is matched.
If a key value with no match is passed, the result is an empty string
$MyString = $MyList.lookup("pig");
→ ""
But if we add a 'default' key/value pair (anywhere in the list):
$MyList = [ant:Wood ant;bee:Carder bee;cow:Jersey;dog:Labrador;default:animal];
and re-run the last example:
$MyString = $MyList.lookup("pig");
→ "animal"
There is still no match but as a default is defined, the default value of "animal" is returned.
More complex and nuanced use of .lookup() is described in the discussion of look-up tables.
Dictionary vs. Lookup
The newer Dictionary data-type offers a more efficient and feature rich way of working with lookup lists.
Legacy use (pre v8)
For look-up tables .lookup() is preferred to the older .at() for clarity, and to avoid ambiguity when the argument is numeric. Using the example list as above:
$MyString = $MyList.at(3);
→ "dog:Labrador", the whole fourth element of the list (do not forget N is counted from zero).
$MyString = $MyList.lookup(3)
→ "animal", the lookup result for key value 5
which doesn't exist, so we get the default.