layout | title | canonical |
---|---|---|
default |
Language: Data Types: Hashes |
/puppet/latest/lang_data_hash.html |
Hashes (sometimes called hash maps) are unordered structures that map keys to values.
Hashes are written as a pair of curly braces containing any number of key/value pairs. A key is separated from its value by a =>
(arrow, 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', }
Hash keys can be any data type, but you should generally only use strings or numbers.
Hash values can be any data type.
You can access hash members with their key; square brackets are used for accessing.
$myhash = { key => "some value",
other_key => "some other value" }
notice( $myhash[key] )
This manifest would log some value
as a notice.
If you try to access a nonexistent key from a hash, its value will be undef
.
$cool_value = $myhash[absent_key] # Value is undef
Nested arrays and hashes can be accessed by chaining indexes:
$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.
The puppetlabs-stdlib module contains several additional functions for dealing with hashes, including:
has_key
is_hash
keys
merge
validate_hash
values
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
will match.
The full signature for Hash
is:
Hash[<KEY TYPE>, <VALUE TYPE>, <MIN SIZE>, <MAX SIZE>]
Although all of these parameters are optional, you must specify both key type and value type if you're going to specify one of them.
Position | Parameter | Data Type | Default Value | Description |
---|---|---|---|---|
1 | Key type | Type |
Scalar |
What kinds of values can be used as keys. Note: 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 | Min Size | Integer |
0 | The minimum number of key/value pairs in the hash. This parameter accepts the special value default , which will use its default value. |
4 | Max Size | Integer |
infinite | The maximum number of key/value pairs in the hash. This parameter accepts the special value default , which will use its default value. |
Hash
--- matches a hash of any length; any keys must matchScalar
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 above, but requires a non-empty hash.Hash[Integer, String, 1, 8]
--- same as above, but 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.
Several abstract types, including Variant
and Enum
, are useful when specifying a value type for hashes that might include multiple kinds of data.