lookup

Reads the value for a key in a dictionary, with an optional fallback.

Category: Language Fundamentals > Data Types

Syntax

v = lookup(D, key)

Returns the value stored under key. An error is raised if the key is absent.

v = lookup(D, key, FallbackValue=fv)

Returns fv for keys that are not in the dictionary.

Description

lookup(D, key) does the same as D(key), but the function form accepts FallbackValue, which makes it convenient for counting or default settings: lookup(D, k, FallbackValue=0) never fails on a new key. Combine it with isKey when a different action is needed for missing keys.

Input arguments

D
A dictionary object.
key
A key, or an array of keys.
FallbackValue
Value to return for missing keys, instead of an error.

Output arguments

v
The value, or an array of values with the shape of key.

Example

A lookup with fallback

d = dictionary(["a" "b"], [10 20]);
lookup(d, "a")
lookup(d, "z", FallbackValue=-1)
Output
ans = 10
ans = -1

Notes

This entry describes the dictionary method. For a containers.Map, read values with M(key) and values(M, keys).

See also

dictionary isKey keys values containers.Map