Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

Loops with for expressions

Sumit Rawal answered on May 14, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers

  • for loop
  • Function with FOR loop
  • simple for loop
  • for loop
  • for...in...loop
  • for loop
  • for loop
  • FOR Loop
  • for loop
  • for loop
  • for loop
  • For-in loop
  • for loop
  • for loop
  • For loop
  • JavaScript for...in loop
  • for loop
  • Javascript "For..in Loop" Syntax
  • Example of a FOR Loop
  • JavaScript for...in loop
  • For Loop
  • for loop
  • for loop
  • how to create a loop with for
  • for loop
  • for loop
  • for loop
  • for loop
  • for loop
  • for loop

  • Loops with for expressions

    0

    You’ve now seen how to use loops to create multiple copies of entire resources and inline blocks, but what if you need a loop to set a single variable or parameter?

    Imagine that you wrote some Terraform code that took in a list of names:

    variable "names" {

    description = "A list of names"

    type = list(string)

    default = ["neo", "trinity", "morpheus"]

    }

    How could you convert all of these names to uppercase? In a general-purpose programming language such as Python, you could write the following for-loop:

    names = ["neo", "trinity", "morpheus"]

    upper_case_names = []

    for name in names:

    upper_case_names.append(name.upper())

    print upper_case_names

    Python offers another way to write the exact for-loop in one line using a syntax known as a list comprehension:

    upper_case_names = [name.upper() for name in names]

    Python also allows you to filter the resulting list by specifying a condition:

    short_upper_case_names = [name.upper() for name in names if len(name) < 5]

    Terraform offers similar functionality in the form of a for expression (not to be confused with the for_each expression you saw in the previous section). The basic syntax of a for expression is as follows:

    [for in : ]  

    Popularity 1/10 Helpfulness 1/10 Language javascript
    Source: Grepper
    Link to this answer
    Share Copy Link
    Contributed on May 14 2023
    Sumit Rawal
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.