GET
lists
maps
binary
Retreive a value in a MAP or a LIST.
The GET
function consumes on the top of the stack a list, a byte array, or a map, and the index (or the key),
then put the result on the stack.
If key is not found, it returns NULL on the stack.
If index is out of bound, GET
raises an error.
Since 2.1, the index can be negative. In this case the effective index is index + size of the LIST or BYTE ARRAY.
Since 2.1, GET
can operate recursively on nested lists. In this case, the index is a list.
Since 2.7.3, GET can operate on a STRING to get a single character, as a STRING. The recursive version can also work on any type supported by GET
as long as the top element is a LIST.
GET is available since version 1.0.0.
See also
Signatures
Examples
// Get a value in a map from the key
{ 'foo' 42 'bar' true }
'foo'
GET
// Get a value in a list from the index
[ 3 12 15 ]
0
GET
// Try to get a value from a non existing key (result will be NULL)
{ 'foo' 42 'bar' true }
33
GET
{ 'audi' 150 'volvo' 120 'dacia' 310 } 'brandcount' STORE // Vehicle count per brand
[ 'dacia' 'audi' 'dacia' 'dacia' 'porsche' 'volvo' 'tesla' ] // New inputs
<%
// Enter the foreach loop, next list value is put on the top of the stack
'brand' STORE // Store the brand
$brandcount $brand GET // Search for existing key. see also CONTAINSKEY
DUP
ISNULL // This test will consume the top of the stack. hence the DUP before.
<% DROP $brandcount 1 $brand PUT %> // Key do not exist, create it with value=1
<% $brandcount SWAP 1 + $brand PUT %> // Key exists, increment the brand count
IFTE
DROP // PUT returns the updated list on the stack. try to comment this line...
// ... you can see that the stack references to the brandcount map.
%> FOREACH
$brandcount // Display the updated brand list
// Recursive GET
[
1337
{
'thekey' 'thevalue'
'anotherkey' 'anothervalue'
}
]
[ -1 'thekey' 3 ] GET
// Leaves the "v" of "thevalue"