Quantcast
Channel: Terraform - HashiCorp Discuss
Viewing all articles
Browse latest Browse all 11460

How do I get the index whilst interating over a list with the For expression?

$
0
0

@arcotek-ltd wrote:

Hi,

Is there a way to get the index number of the current item whilst iterating through a list with the For expression. Similar to how {iterator}.key works with for_each?: https://discuss.hashicorp.com/t/iteration-counter-in-dynamic-blocks/1638.

Basically, I have n items in a list but don’t want to have to provide an attribute telling it what number in the list it is. In the below snippet, s.key doesn’t work, but hopefully it shows what I’m trying to achieve.

dynamic "subnet" {
for_each = [for s in each.value["subnets"]: {
  name   = format("%s%02d",s.name, s.key)
  prefix = cidrsubnet(element(each.value["address_space"],0), s.newbits, s.key)
  
}]

In PowerShell, I would do something like:

$list = @("Cat","Dog","Fish","Foo")

For ($i=0; $i -lt $list.Length; $i++)
{
    "$i $($list[$i])"
}

I’ve tried s, s.index, count

Any help, greatfully received.

TIA

W.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 11460

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>