Hashes
Hashes map keys to values, maintaining the order of the entries according to insertion order.
Syntax
{}
containing any number of key-value pairs. A key is separated from its value by an arrow
(sometimes called a fat comma or hash rocket) =>
, and
adjacent pairs are separated by commas. An optional trailing comma is allowed between
the final value and the closing curly
brace.{ key1 => 'val1', key2 => 'val2' }
# Equivalent:
{ key1 => 'val1', key2 => 'val2', }
{ 'key1' => ['val1','val2'],
'key2' => { 'key3' => 'val3', },
'key4' => true,
'key5' => 12345,
}
Accessing values
$myhash = { key => "some value",
other_key => "some other value" }
notice( $myhash[key] )
This example manifest would log some value
as a notice.undef
:$cool_value = $myhash[absent_key] # Value is undef
$main_site = { port => { http => 80,
https => 443 },
vhost_name => 'docs.puppetlabs.com',
server_name => { mirror0 => 'warbler.example.com',
mirror1 => 'egret.example.com' }
}
notice ( $main_site[port][https] )
This example manifest would
log 443
as a notice.Merging hashes
When hashes are merged (using the addition (+
)
operator), the keys in the constructed hash have the same order as in the original
hashes, with the left hash keys ordered first, followed by any keys that appeared only
in the hash on the right side of the merge.
+
). For
example:$values = {'a' => 'a', 'b' => 'b'}
$overrides = {'a' => 'overridden'}
$result = $values + $overrides
notice($result)
-> {'a' => 'overridden', 'b' => 'b'}
The Hash
data type
The data type of hashes is Hash
. By default, Hash
matches hashes of any size, as long as
their keys match the abstract type Scalar
and their values match the abstract type Data
. You can use parameters to restrict which
values Hash
matches.
Parameters
Hash
is:Hash[<KEY TYPE>, <VALUE TYPE>, <MIN SIZE>, <MAX SIZE>]
These
parameters are optional. You must specify both key type and value type if
you’re going to specify one of them. The parameters must be listed in order; if you need
to specify a later parameter, you must also specify values for any prior ones. Position | Parameter | Data type | Default value | Description |
---|---|---|---|---|
1 | Key type | Type |
Scalar |
What kinds of values can be used as keys. If you specify a key type, a value type is mandatory. |
2 | Value type | Type |
Data |
What kinds of values can be used as values. |
3 | Minimum size | Integer |
0 |
The minimum number of key-value pairs in the hash. This parameter
accepts the special value default ,
which uses its default value. |
4 | Maximum size | Integer |
infinite | The maximum number of key-value pairs in the hash. This parameter
accepts the special value default ,
which uses its default value. |
-
Hash
- Matches a hash of any length; all keys must match
Scalar
and any values must matchData
. -
Hash[Integer, String]
- Matches a hash that uses integers for keys and strings for values.
-
Hash[Integer, String, 1]
- Same as previous, and requires a non-empty hash.
-
Hash[Integer, String, 1, 8]
- Same as previous, and with a maximum size of eight key-value pairs.
The abstract Struct
data type lets you
specify the exact keys allowed in a hash, as well as what value types are allowed for
each key.
Other abstract types, particularly Variant
and Enum
, are useful when
specifying a value type for hashes that include multiple kinds of data.