How to store maps' values and not the map itself?

  • I have a shortcode : It process a CSV file and he tracks column sums for each value of a certain critera : (myCritera; item 1; Item 2; item N).
  • myCritera unique values are : ValueA, ValueB, … ValueZ
  • Informations about CSV file and column number to be processed are all in a front matter .md. And number of column is variable.
  • Finally I get a map for column Item 1 : item 1:[valueA:sumA ValueB:sumB ...]. It works fine.

But to achieve that I use $.Scratch.SetInMap "myTempMap" to store the temporay map.
And reuse this myTempMap on my range iteration over each column Item N

Problem is that it store the map and not the maps’s value. So each iteration change the maps I previously stored. And finaly I get a nice map but with the same values stored !!

I tried to use a dict but i can’t add some value to a dict without using Scratch.SetInMap => catch 22.
I guess I miss something obvious, … but what ??

EDIT : And as my number of values is unknown, I can’t use prenamed maps (which would make my code work)

In my front matter I have [11, 12, 17] for example = > $listeColStats


{{- range $k, $codeStatTemp:= $listeColStats -}}
  
    {{- range $listeCodes1Categ -}}
        {{- $.Scratch.SetInMap "mapSommeCateg_1total_Temp" . 0.0 -}}
    {{- end -}}

        {{- $csv := getCSV  ";" ($.Scratch.Get "csvURL") -}}
        {{- range $i, $ligneTemp := $csv -}}
            {{- if gt $i 0 -}}
 
               {{- $codeCategTemp := (index $ligneTemp $numcol_categ) -}}
                    {{- $val_FR_str := index $ligneTemp $codeStatTemp -}}
                    {{- $val_US_str := replaceRE "," "." $val_FR_str -}}
                {{- $val2add := float (lang.NumFmt 2 $val_US_str "- .") -}}
                {{- $valOrigineCol := (index ($.Scratch.Get "mapSommeCateg_1total_Temp") $codeCategTemp) -}}
 
                {{- $valResult := add $val2add $valOrigineCol -}}
 
               {{- $.Scratch.SetInMap "mapSommeCateg_1total_Temp" $codeCategTemp $valResult -}}

            {{- else -}}
 
            {{- end -}}     <!-- Fin de test en-tête -->
        {{- end -}}         <!-- Fin de range CSV -->

        <!-- HERE IS THE VALUE I WANT TO KEEP AFTER EACH ITERATION -->
        {{ $_ := ($.Scratch.Get "mapSommeCateg_1total_Temp") }}
        {{- $.Scratch.SetInMap "mapStatTotaux" (string $codeStatTemp) $_ -}}

{{- end -}}   

`
``
1 Like

Something like that ?

The result is

map[foo:map[alpha:0 beta:1 gamma:2] bar:map[alpha:3 beta:4 gamma:5] baz:map[gamma:8 alpha:6 beta:7]]

I don’t know if it’s the best way to do it… but it does the trick.

<div id="dictdemo">
    {{ $mainKeys := (slice "foo" "bar" "baz") }}
    {{ $.Scratch.Set "mainDict" (dict) }}
    {{ $.Scratch.Set "counter" 0 }}
    {{ range $mainKeys }}

        {{ $.Scratch.Set "subDict" (dict) }}
        {{ $.Scratch.SetInMap "subDict" "alpha" ($.Scratch.Get "counter") }}
        {{ $.Scratch.Add "counter" 1 }}
        {{ $.Scratch.SetInMap "subDict" "beta" ($.Scratch.Get "counter") }}
        {{ $.Scratch.Add "counter" 1 }}
        {{ $.Scratch.SetInMap "subDict" "gamma" ($.Scratch.Get "counter") }}
        {{ $.Scratch.Add "counter" 1 }}

        {{ $.Scratch.SetInMap "mainDict" . ($.Scratch.Get "subDict") }}
    {{ end }}

    {{/* render awfully the map of maps */}}
    {{ $.Scratch.Get "mainDict" }}
</div>
2 Likes

Thanks Sam.
I succeeded with using a variable in my temporary Map name, but I’ll implement your solution.