# Test for empty array with range

**URL:** https://discourse.gohugo.io/t/test-for-empty-array-with-range/2660
**Category:** support
**Created:** [February 10, 2016, 7:29pm UTC](https://discourse.gohugo.io/t/test-for-empty-array-with-range/2660 "2016-02-10T19:29:26Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![mrsmith](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@mrsmith](https://discourse.gohugo.io/u/mrsmith)
#### Post date: [February 10, 2016, 7:29pm UTC](https://discourse.gohugo.io/t/test-for-empty-array-with-range/2660/1 "2016-02-10T19:29:26Z")

</div>

My frontmatter is being generated by a go program using [https://github.com/BurntSushi/toml](https://github.com/BurntSushi/toml) and it is automatically putting empty arrays when there are no vegetables or fruits in other cases.

For example, in my frontmatter I have the following:

```
[food]
  fruits = ["pear", "banana", "pineapple"]
  vegetables = [""]

```

In my `layout/fruit/single.html` I have this bit of code

```
{{range $key, $val := .Params.food}}
{{if not (eq $val "")}}
<span>{{delimit $val " · "}}</span>
{{end}}
{{end}}

```

The problem is that the resulting HTML has two `<span>` elements even though the `.Params.food.vegetables` is an empty array.

If I change my frontmatter to this:

```
[food]
  fruits = ["pear", "banana", "pineapple"]
  vegetables = "" <---- CHANGED HERE

```

The resulting HTML only has one `<span>` as desired.

How can I test for the empty array and only generate one `<span>` element?

UPDATE: Even the length test doesn’t work because [""] in the frontmatter is considered greater than 0, e.g.

{{range $key, $val := .Params.food}}  
{{if gt (len $val) 0}}  
{{delimit $val " · "}}  
{{end}}  
{{end}}

---

<div class="post-metadata">

### Author: ![moorereason](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/moorereason/32/592_2.png) [@moorereason](https://discourse.gohugo.io/u/moorereason)
#### Post date: [February 10, 2016, 8:19pm UTC](https://discourse.gohugo.io/t/test-for-empty-array-with-range/2660/2 "2016-02-10T20:19:12Z")

</div>

Try something like this:

```auto
{{ range $type, $val := .Params.food }}
{{ if gt (len $val) 0 }}{{ if not (eq (index $val 0) "") }}
<span>{{ delimit $val " . " }}</span>
{{ end }}{{ end }}

```

Note that `$type` will be `fruits` and `vegetables`, and `$val` will be the arrays.

---

<div class="post-metadata">

### Author: ![mrsmith](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@mrsmith](https://discourse.gohugo.io/u/mrsmith)
#### Post date: [February 10, 2016, 11:52pm UTC](https://discourse.gohugo.io/t/test-for-empty-array-with-range/2660/3 "2016-02-10T23:52:44Z")

</div>

Thank you that worked great 😌

Btw I combined the two ifs like this:

`{{if and (gt (len $val) 0) (not (eq (index $val 0) ""))}}`
