Range over getCSV array not iterating

I am attempting to import a csv file of contact information :card_index: similar to the example below:

Name,(555)123-4567,email1@domain.com
AnotherName,(555)123-4568,email2@domain.com
YetAnotherName,(555)123-4569,email3@domain.com

It is broken up by carriage returns "\r" and then taking the resulting array and using split to seperate by commas "," in order to output the resulting values with index for a large data set of contact information. Each line is a contact, each value between commas are contact information (i.e. name, phone number, email)

  {{range $i, $r := getCSV "\r" "/rolodex.csv"}}
  {{$c := split (index $r $i) ","}}
  <dt>{{index $c 0}}</dt>
  <dd>{{index $c 1}}<dd>
  <dd>{{index $c 2}}<dd>
  {{end}}

when I run hugo the output html shows the first row of $r and the resulting data is correctly split, however the range does not iterate through $i the value of $i remains 0. The output looks like below for the above data example:

<dt>Name</dt>
<dd>(555)123-4567</dd>
<dd>email1@domain.com</dd>

Please help me realize what I am missing.

I haven’t got time to answer specifically your scenario, but I had an initial play with this a couple of days ago to produce a table of data from a csv. Here is a snippet of code I have stored away for later adaptation. It may or may not help:

<div class="responsive-embed">
<table border="1">

    {{ range $i, $r := getCSV .Site.Params.sep .Site.Params.url }}
    <thead>
    {{ if eq $i 0 }}
    {{ range $r }}
    <th>{{ . }}</th>
    {{end}}
    {{end}}
    </thead>
    {{ if ge $i 1 }}
    <tr>
        {{ range  $r }}
        <td>{{ . }}</td>
        {{end}}
    </tr>
    {{end}}
    {{ end }}
</table>
</div>

Edit: Yes, try the above, with the comma being the separator. Ignore the returns, as my csv was laid out like that and the above code worked.

1 Like

Thanks for the quick reply @Jonathan_Griffin , your code suggestion gave me the idea to do some testing and see what the {{ . }} dot contained at different points. I found that it was iterating correctly, I just wasn’t accessing the data correctly.

I first tried replacing the (index $r $i) function with the . alone as {{$c := split . ","}} however it was throwing the following error:

error calling split: unable to cast []string
{"Name,(555)123-4567,email1@domain.com"} 
of type []string to string

So in order to access what was in . from split I needed to access the string within []string so i used (index . 0) to get it in the right format for split to separate by commas. The code I got working is below:

{{range $i, $r := getCSV "\r" "/rolodex.csv"}}
{{$c := split (index . 0) ","}}
<dt>{{index $c 0}}</dt>
<dd>{{index $c 1}}<dd>
<dd>{{index $c 2}}<dd>
{{end}}

This code breaks up the CSV file into rows by each carriage return, "\r" and columns from each comma "," allowing you to iterate through all the rows and place specific column data in different parts of the range contents using index. This makes it easy to use data from a csv that is grouped by line with different types of values in different columns, and also be able to have multiple tables in a single csv file (each line a table with the first column {{index $c 0}} as the table header). I hope this helps someone else looking for an answer to this problem.


Update:
further explanation here: [Solved] CSV data key lookup - #3 by O_O