# How counting up a variable in range

**URL:** <https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586>\
**Category:** support\
**Created:** [January 13, 2015, 9:18pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586 "2015-01-13T21:18:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![zwotzie](https://avatars.discourse-cdn.com/v4/letter/z/edb3f5/32.png) [@zwotzie](https://discourse.gohugo.io/u/zwotzie)\
**Post date:** [January 13, 2015, 9:18pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/1 "2015-01-13T21:18:32Z")

</div>

Sometimes you need a simple counter… for example unique

```
id="myid{{ $count }}"

```

I thought about (simplified):

```
{{ $count := 1 }}
...
{{ range $value.Pages }} 
    {{ $count := add $count 1 }}
         <div id="myuid{{ $count }}">...</div>
{{ end }}

```

but everything i tried like in other languages failed or gives me always back the number 2  
Is there a solution in hugo (0.12)?

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [January 13, 2015, 9:20pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/2 "2015-01-13T21:20:49Z")

</div>

No. This is a limitation with Go templates.

But there is a suggested solution to this here:

> <https://github.com/gohugoio/hugo/pull/746>

---

<div class="post-metadata">

**Author:** ![zwotzie](https://avatars.discourse-cdn.com/v4/letter/z/edb3f5/32.png) [@zwotzie](https://discourse.gohugo.io/u/zwotzie)\
**Post date:** [January 13, 2015, 9:27pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/3 "2015-01-13T21:27:09Z")

</div>

ok, thats cracy. That means for me (and others), that i have to write a script, which generates a uniq\_id and write that in the coresponding front matter.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [January 13, 2015, 9:39pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/4 "2015-01-13T21:39:13Z")

</div>

OK, I did not look too hard on your question. Your construction with the counter variable doesn’t work.

But you can achieve similar by adding the index variable to your range construct.

```
{{ range $index, $element := $value.Pages }}

```

EDIT IN: This will start at 0, but you can use “add” to add 1.

---

<div class="post-metadata">

**Author:** ![zwotzie](https://avatars.discourse-cdn.com/v4/letter/z/edb3f5/32.png) [@zwotzie](https://discourse.gohugo.io/u/zwotzie)\
**Post date:** [March 15, 2015, 6:48pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/5 "2015-03-15T18:48:57Z")

</div>

for hugo 0.12 this one worked fine:

```
{{ range $index, $page := $value.Pages }}
{{ $c :=add $index 1 }}
{{ $c }}= value of $c... 
    {{ if eq ( mod $c 4 ) 1 }}
         {{ $c }} first one of each 4... ($c=1,5,...)
    {{ end }}
{{ end }}

```

$index counts up as expected, {{ $c }} prints out the value of c, but the if-clause is in 0.13 broken.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [March 15, 2015, 7:10pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/6 "2015-03-15T19:10:19Z")

</div>

Reported and fixed in [https://github.com/spf13/hugo/issues/961](https://github.com/spf13/hugo/issues/961)

---

<div class="post-metadata">

**Author:** ![zwotzie](https://avatars.discourse-cdn.com/v4/letter/z/edb3f5/32.png) [@zwotzie](https://discourse.gohugo.io/u/zwotzie)\
**Post date:** [March 15, 2015, 8:47pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/7 "2015-03-15T20:47:38Z")

</div>

after reading the reported issue and the coresponding discussion, maybe someone find my temporary workaround useful: don’t mix up fixed/raw numbers with calculated ones. Use only calculated…

instead of

```
{{ if eq ( mod $c 4 ) 1 }}

```

use:

```
{{ if eq ( mod $c 4 ) (add 1 0) }}

```

but {{ add 0 0 }} is not working for 0, use {{ sub 1 1 }} …

this works for me 😉

---

<div class="post-metadata">

**Author:** ![chakmear](https://avatars.discourse-cdn.com/v4/letter/c/6a8cbe/32.png) [@chakmear](https://discourse.gohugo.io/u/chakmear)\
**Post date:** [July 11, 2018, 1:42pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/8 "2018-07-11T13:42:02Z")

</div>

I found another method without depending on the index var which might be at times a string when having a dictionary, and also with being able to sort array…

```
  {{ $.Scratch.Set "counter" 0 }}
  {{range sort .Params.employees}}
    {{ $.Scratch.Set "counter" (add ($.Scratch.Get "counter") 1) }}
    I am the number {{$.Scratch.Get "counter"}} in loop!
  {{end}}
```

---

<div class="post-metadata">

**Author:** ![short](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/short/32/11382_2.png) [@short](https://discourse.gohugo.io/u/short)\
**Post date:** [May 26, 2020, 12:40pm UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/9 "2020-05-26T12:40:18Z")

</div>

Terrific!!! Thanks I’m using it now

---

<div class="post-metadata">

**Author:** ![toledox82](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/toledox82/32/18180_2.png) [@toledox82](https://discourse.gohugo.io/u/toledox82)\
**Post date:** [June 22, 2023, 3:46am UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/10 "2023-06-22T03:46:05Z")

</div>

Just a small fix, you can use Add method directly.

```auto
{{ $.Scratch.Set "counter" 0 }}
{{range sort .Params.employees}}
   {{ $.Scratch.Add "counter" 1) }}
   I am the number {{$.Scratch.Get "counter"}} in loop!
{{end}}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 22, 2023, 4:07am UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/11 "2023-06-22T04:07:04Z")

</div>

This thread is really old, Go has changed, and you don’t need to initialize a Scratch to count.

```plaintext
{{ $i := 0 }}
{{ range $foo }}
  {{ $i = add $i 1 }}
{{ end }}

{{ $globalPopulation := 0 }}
{{ range $nations }}
  {{ $globalPopulation = add $globalPopulation .population }}
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [June 22, 2023, 4:07am UTC](https://discourse.gohugo.io/t/how-counting-up-a-variable-in-range/586/12 "2023-06-22T04:07:09Z")

</div>


