github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/website/content/docs/job-specification/hcl2/functions/collection/element.mdx (about)

     1  ---
     2  layout: docs
     3  page_title: element - Functions - Configuration Language
     4  description: The element function retrieves a single element from a list.
     5  ---
     6  
     7  # `element` Function
     8  
     9  `element` retrieves a single element from a list.
    10  
    11  ```hcl
    12  element(list, index)
    13  ```
    14  
    15  The index is zero-based. This function produces an error if used with an
    16  empty list.
    17  
    18  Use the built-in index syntax `list[index]` in most cases. Use this function
    19  only for the special additional "wrap-around" behavior described below.
    20  
    21  ## Examples
    22  
    23  ```shell-session
    24  > element(["a", "b", "c"], 1)
    25  b
    26  ```
    27  
    28  If the given index is greater than the length of the list then the index is
    29  "wrapped around" by taking the index modulo the length of the list:
    30  
    31  ```shell-session
    32  > element(["a", "b", "c"], 3)
    33  a
    34  ```
    35  
    36  ## Related Functions
    37  
    38  - [`index`](/docs/job-specification/hcl2/functions/collection/index-fn) finds the index for a particular element value.
    39  - [`lookup`](/docs/job-specification/hcl2/functions/collection/lookup) retrieves a value from a _map_ given its _key_.