frawk
frawk copied to clipboard
what is the proper way to use multidimensional maps
I'm having difficulty generating multidimensional maps with frawk.
let's try to get yearly sales report for some products. here's data
$ echo -n -e "year,product,sold\n2021,A,50\n2021,B,100\n2021,A,70\n2020,A,10" > sales.csv
$ cat sales.csv
year,product,sold
2021,A,50
2021,B,100
2021,A,70
2020,A,10
with awk we can do
$ awk -F"," 'NR>1 {sales[$1][$2] += $3}END{for (year in sales){ for (prod in sales[year]){print year, prod, sales[year][prod]}}}' sales.csv
2020 A 10
2021 A 120
2021 B 100
let's try with frawk (I omitted print part, just trying to build the map)
$ cat sales.csv | frawk -icsv -H '{sales[$1][$2] += $3}'
error compiling cranelift: [src/types.rs:525:40] kinds do not match. Map { key: None, val: Some(Int) } vs Scalar(None)
what is the trick to generate multidimensional maps in frawk? do we need some sort of initialization at the beginning?