How can I start a sequence with 1?

I learned that for loops are create by

 range (seq 20)

This starts a loop at 0. How can I start at 1?

https://hugodocs.info/functions/after/

1 Like

thx @rdwatters

1 Like

That is correct, but you would then have to know that

range after 1 (seq 20)

will return 19 elements.

If you range with an index, I think it is less confusing to do something like this:

{{ range $index, $num := (seq 20) }}
$indexStartingAt1 := (add $index 1)
{{ end }}

In the above, both $indexStartingAt1 and $num will be 1,2,3 … 20.

2 Likes

Awesome, @bep. I added this example. What do you think?

https://hugodocs.info/functions/seq/

2 Likes

And since both numbers now are equal, you can drop one of them:

{{ range (seq 20) }}
{{ . }}
{{ end }}

Will print 1…20.

1 Like

very helpful, thank you!