Open source Puppet documentation

Arrays are ordered lists of values. Resource attributes which accept multiple values (including the relationship metaparameters) generally expect those values in an array. Many functions also take arrays, including the iteration functions.

Arrays are written as comma-separated lists of values surrounded by square brackets, []. An optional trailing comma is allowed between the final value and the closing square bracket.
[ 'one', 'two', 'three' ]
# Equivalent:
[ 'one', 'two', 'three', ]
The values in an array can be any data type.

Accessing items in an array

You can access items in an array by their numerical index, counting from zero. Square brackets are used for accessing. For example:
$foo = [ 'one', 'two', 'three' ]
notice( $foo[1] )
This manifest would log two as a notice. The value of $foo[0] would be one, because indexes count from zero.
The opening square bracket must not be preceded by a white space:
$foo = [ 'one', 'two', 'three', 'four', 'five' ]
notice( $foo[2] )  # ok
notice( $foo [2] ) # syntax error
Nested arrays and hashes can be accessed by chaining indexes:
$foo = [ 'one', {'second' => 'two', 'third' => 'three'} ]
notice( $foo[1]['third'] )
This manifest would log three as a notice. $foo[1] is a hash, and we access a key named 'third'.
Arrays support negative indexes, counting backward from the last element, with -1 being the last element of the array:
$foo = [ 'one', 'two', 'three', 'four', 'five' ]
notice( $foo[2] )
notice( $foo[-2] )
The first notice would log three, and the second notice would log four.
If you try to access an element beyond the bounds of the array, its value will be undef:
$foo = [ 'one', 'two', 'three', 'four', 'five' ]
$cool_value = $foo[6] # value is undef
When testing with a regular expression whether an Array[<TYPE>] data type matches a given array, empty arrays will match if the type accepts zero-length arrays.
$foo = []
if $foo =~ Array[String] {
  notice( 'foo' )
This manifest would log foo as a notice, because the regular expression matches the empty array.

Accessing sections of an array

You can access sections of an array by numerical index. Like accessing individual items, accessing sections uses square brackets, but it uses two indexes, separated by a comma. For example: $array[3,10].

The result of an array section is always another array.

The first number of the index is the start position. Positive numbers count forward from the start of the array, starting at zero. Negative numbers count backward from the end of the array, starting at -1.

The second number of the index is the stop position. Positive numbers are lengths, counting forward from the start position. Negative numbers are absolute positions, counting backward from the end of the array starting at -1.

For example:
$foo = [ 'one', 'two', 'three', 'four', 'five' ]
notice( $foo[2,1] )  # evaluates to ['three']
notice( $foo[2,2] )  # evaluates to ['three', 'four']
notice( $foo[2,-1] ) # evaluates to ['three', 'four', 'five']
notice( $foo[-2,1] ) # evaluates to ['four']

Array operators

You can use operators with arrays to create new arrays:
  • append with <<

  • concatenate with +

  • remove elements with -

or to convert arrays to comma-separated lists with * (splat). See the Array operators section of Expressions for more information.

Additional functions for arrays

The puppetlabs-stdlib module contains useful functions for working with arrays, including:
delete join size
delete_at member sort
flatten prefix unique
grep range validate_array
hash reverse values_at
is_array shuffle zip

The Array data type

The data type of arrays is Array. By default, Array matches arrays of any length, provided all values in the array match the abstract data type Data. You can use parameters to restrict which values Array will match.


The full signature for Array is:
These parameters are optional. They must be listed in order; if you need to specify a later parameter, you must also specify values for any prior ones.
PositionParameterData typeDefault valueDescription
1Content typeTypeDataThe kind of values the array contains. You can specify only one data type per array, and every value in the array must match that type. Use an abstract type to allow multiple data types. If the order of elements matters, use the Tuple type instead of Array.
2Minimum sizeInteger0The minimum number of elements in the array. This parameter accepts the special value default, which will use its default value.
3Maximum sizeIntegerinfiniteThe maximum number of elements in the array. This parameter accepts the special value default, which will use its default value.
Matches an array of any length. All elements in the array must match Data.
Matches an array of any size that contains only strings.
Array[Integer, 6]
Matches an array containing at least six integers.
Array[Float, 6, 12]
Matches an array containing at least six and at most 12 floating-point numbers.
Array[Variant[String, Integer]]
Matches an array of any size that contains only strings or integers, or both.
Array[Any, 2]
Matches an array containing at least two elements, allowing any data type (including Type and Resource).

The abstract Tuple data type lets you specify data types for every element in an array, in order. Abstract types, such as Variant and Enum, are useful when specifying content types for arrays that include multiple kinds of data.

Back to top