-
Notifications
You must be signed in to change notification settings - Fork 5
/
csv_test.nim
38 lines (28 loc) · 899 Bytes
/
csv_test.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import os, strutils, streams, tables, parsecsv
const
Delim = '\t'
proc main() =
if paramCount() < 3:
quit("synopsis: " & getAppFilename() & " filename keyfield valuefield")
let
filename = paramStr(1)
keyFieldIndex = parseInt(paramStr(2))
valueFieldIndex = parseInt(paramStr(3))
maxFieldIndex = max(keyFieldIndex, valueFieldIndex)
var
sumByKey = newCountTable[string]()
file = newFileStream(filename, fmRead)
if file == nil:
quit("cannot open the file " & filename)
defer: file.close()
var csv: CsvParser
open(csv, file, filename, separator=Delim)
while csv.readRow():
if len(csv.row) > maxFieldIndex:
sumByKey.inc(csv.row[keyFieldIndex], parseInt(csv.row[valueFieldIndex]))
if sumByKey.len() == 0:
echo "No entries"
else:
let largest = sumByKey.largest()
echo "max_key: ", largest[0], " sum: ", largest[1]
main()