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

Error in tutorial regarding slice() function it appears

$
0
0

Hi!

I was reading this part of a tutorial located here:

The slice() function takes three arguments: the list to slice, the index to start from, and the number of elements. It returns a new list with the specified elements copied (“sliced”) from the original list.

Is that last argument really the number of elements? According to the documentation for slice(), that last argument is actually the last element exclusive. Which also explains why for an array of 8 elements and thus index 0 to 7, I can still put in a last argument of 8 and it doesn’t error out. But if I do 9, the it does error out:

> var.private_subnet_cidr_blocks
[
  "10.0.101.0/24",
  "10.0.102.0/24",
  "10.0.103.0/24",
  "10.0.104.0/24",
  "10.0.105.0/24",
  "10.0.106.0/24",
  "10.0.107.0/24",
  "10.0.108.0/24",
]
> slice(var.private_subnet_cidr_blocks, 3, 7)
[
  "10.0.104.0/24",
  "10.0.105.0/24",
  "10.0.106.0/24",
  "10.0.107.0/24",
]
> slice(var.private_subnet_cidr_blocks, 3, 8)
[
  "10.0.104.0/24",
  "10.0.105.0/24",
  "10.0.106.0/24",
  "10.0.107.0/24",
  "10.0.108.0/24",
]
> slice(var.private_subnet_cidr_blocks, 3, 9)

>  
Error: Invalid function argument

  on <console-input> line 1:
  (source code not available)

Invalid value for "end_index" parameter: end index must not be greater than
the length of the list.

Also, since the last argument is the end index exclusive and not the number of elements to grab after the start index - the second argument to slice() - I can’t do the below:

> slice(var.private_subnet_cidr_blocks, 3, 2)

>

**Error:** **Invalid function argument**

on <console-input> line 1:

(source code not available)

Invalid value for "start_index" parameter: start index must not be greater

than end index.

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 11407

Trending Articles



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