frawk icon indicating copy to clipboard operation
frawk copied to clipboard

what is the proper way to use multidimensional maps

Open alperyilmaz opened this issue 3 years ago • 3 comments

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?

alperyilmaz avatar Sep 04 '21 01:09 alperyilmaz

Hi!

frawk does not support multidimensional maps. Maps containing maps is a gawk-specific feature, other dialects of awk (including mawk, and "one true awk") do not support them. My aims so far have been to get the "book awk" versions of the language solid before moving onto common extensions.

With that said, I recognize that maps of maps are extremely useful, and I'm happy to keep this open as a feature request. It will, however, take some time to implement this properly

ezrosent avatar Sep 05 '21 18:09 ezrosent

I should add that in this case, something like frawk -icsv { sales[$1,$2] += $3; } should work. But then you have to manually split by SUBSEP to access year and prod again, which is both annoying to write, and inefficient. That's one of my big reasons for wanting #58. But even with better syntax for splitting by SUBSEP, the mechanism is much less powerful than full multidimensional maps.

ezrosent avatar Sep 05 '21 18:09 ezrosent

I see.. I must have been using gawk for long time thinking that it's the awk, I didn't know that "true awk" does not support this feature. Nevertheless, it's good to know there's workaround by concatenating keys. Thanks for keeping this open as a request.

alperyilmaz avatar Sep 05 '21 19:09 alperyilmaz