I improved a little bit on my recent thread <https://forum.nim-lang.org/t/6403> to have awk-style text processing in Nim, mostly to remove boilerplate. I ended up with a really thin wrapper script to add some modules and the header, as well as accept code as a parameter, getting a bit closer to awk-level convenience (although I'm not sure about how one-liners are going to square with Nim syntax). It's really thin, but it does seem useful enough to be in its own package. I called it naw, a little less than awk but with Nim.
https://nimble.directory/pkg/naw
The naw module is imported by default and includes a convenience iterator, rows, so you can do this:
echo 'foo,1
bar,2
fuz,3
buz,4' | naw -F, 'My Report
===========
{"":-<20} {"":-<20}
# for r in rows():
{r[0]:<20} {r[1]:>20}
# end
{"":-<20} {"":-<20}
# for r in rows():
{r[0]} {r[1]}
# end
'
And come up with this
My Report
===========
-------------------- --------------------
foo 1
bar 2
fuz 3
buz 4
-------------------- --------------------
Same as last time, but with less stuff to type, and on the command line. You can also go through a whole bunch of CSV files using several iterators with filenames, for example to combine the data
myfile.csv
a,1
b,2
c,3
anotherfile.csv
e,1
f,2
g,3
naw -F:, '
# for r in rows("myfile.csv"):
{r[0]}:
# for s in rows("anotherfile.csv"):
{s[0]} {parseInt(s[1]) + parseInt(r[1])}
# end
# end
'
output
a:
e 2
f 3
g 4
b:
e 3
f 4
g 5
c:
e 4
f 5
g 6
includes a convenience iterator, rows,
Looking at your example code, I think that rows should be cols? Because in your example, r[0] points to the first field or column and r[1] points to the second?
rows should be cols
Nope! rows iterates through all rows, r is a single row, which holds the columns for that row. It could be the other way around if I had a good enough readon to, but then I would have to possibly buffer the input and loop through it once for every column instead of only once.