summary refs log tree commit diff stats
path: root/examples/statcsv.nim
blob: 48c4ea0ccd7259dfa75d734ea6cd5c100fa8fe1f (plain) (blame)
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# Example program to show the parsecsv module
# This program reads a CSV file and computes sum, mean, minimum, maximum and
# the standard deviation of its columns.
# The CSV file can have a header which is then used for the output.

import os, streams, parsecsv, strutils, math

if paramCount() < 1:
  quit("Usage: statcsv filename[.csv]")

var filename = addFileExt(ParamStr(1), "csv")
var s = newFileStream(filename, fmRead)
if s == nil: quit("cannot open the file " & filename)

var
  x: TCsvParser
  header: seq[string]
  res: seq[TRunningStat]
open(x, s, filename, separator=';', skipInitialSpace = true)
while readRow(x):
  if processedRows(x) == 1:
    newSeq(res, x.row.len) # allocate space for the result
    if validIdentifier(x.row[0]):
      # header line:
      header = x.row
    else:
      newSeq(header, x.row.len)
      for i in 0..x.row.len-1: header[i] = "Col " & $(i+1)
  else:
    # data line:
    for i in 0..x.row.len-1: 
      push(res[i], parseFloat(x.row[i]))
x.close()

# Write results:
for i in 0..header.len-1:
  stdout.write("\t")
  stdout.write(header[i])
stdout.write("\nSum")
for i in 0..header.len-1:
  stdout.write("\t")
  stdout.write(res[i].sum)
stdout.write("\nMean")
for i in 0..header.len-1:
  stdout.write("\t")
  stdout.write(res[i].mean)
stdout.write("\nMin")
for i in 0..header.len-1:
  stdout.write("\t")
  stdout.write(res[i].min)
stdout.write("\nMax")
for i in 0..header.len-1:
  stdout.write("\t")
  stdout.write(res[i].max)
stdout.write("\nStdDev")
for i in 0..header.len-1:
  stdout.write("\t")
  stdout.write(res[i].standardDeviation)
stdout.write("\n")
ass="w"> = 1; twa.background_pixmap = ParentRelative; twa.event_mask = ExposureMask; c->title = XCreateWindow(dpy, root, c->r[RFloat].x, c->r[RFloat].y, c->r[RFloat].width, barrect.height, 0, DefaultDepth(dpy, screen), CopyFromParent, DefaultVisual(dpy, screen), CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa); XFlush(dpy); #if 0 for(t=&client, i=0; *t; t=&(*t)->next, i++); c->next = *t; /* *t == nil */ *t = c; #endif return c; } void manage(Client *c) { XMapRaised(dpy, c->win); XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime); XFlush(dpy); } Client * getclient(Window w) { Client *c; for(c = clients; c; c = c->next) if(c->win == w) return c; return NULL; }