A YCP
-map is an associative array. It is a list of key-value-pairs
with the keys being non-ambiguous, i.e. there are no two keys being
exactly equal. While you can use values of any type for keys and
values, you should restrict the keys to be of
type string because from experience other types
tend to complicate the code. Values of arbitrary
type on the other hand make the map a very flexible data container.
Maps are denoted with $[ key_0:value_0, key_1:value_1,
...]
. The empty map is denoted by $[]
.
Note: A map does not reserve order of its elements when iterating over them.
Example 2.7. Map constants
$[ ] $[ "/usr": 560, "/home" : 3200 ] $[ "first": true, "2": [ true, false ], "number" : 8+9 ]
Accessing the map elements is done by means of the index operator as
in my_map["os_type"]:"linux"
. This returns the
value associated with the key "os_type"
. As
with lists, a default value must be appended
(after a colon) that is returned if the given key does not
exist. Again it should have the type that is expected for the
current access, in this case the string “linux”.
You may have noticed that the syntax for accessing maps kind of resembles that of accessing lists. This is due to the fact that lists are realized as maps internally with constant keys 0, 1, 2, and so on.
Note: There is also another method for accessing maps, originating
from the early days of YaST
. The command lookup(my_map,
"os_type", "linux")
also returns the value associated
with the given key. While this still works, it is deprecated by now
and may be dropped in the future.