- The OpenTofu Language
- Functions
- matchkeys
matchkeys
Function
matchkeys
constructs a new list by taking a subset of elements from one
list whose indexes match the corresponding indexes of values in another
list.
matchkeys
identifies the indexes in keyslist
that are equal to elements of
searchset
, and then constructs a new list by taking those same indexes from
valueslist
. Both valueslist
and keyslist
must be the same length.
The ordering of the values in valueslist
is preserved in the result.
Examples
If the result ordering is not significant, you can achieve a similar result
using a for
expression with a map:
If the keys and values of interest are attributes of objects in a list of
objects then you can also achieve a similar result using a for
expression
with that list:
For example, the previous form can be used with the list of resource instances
produced by a resource
block with the count
meta-attribute set, to filter
the instances by matching one of the resource attributes:
Since the signature of matchkeys
is complicated and not immediately clear to
the reader when used in configuration, prefer to use for
expressions where
possible to maximize readability.