CSV data manipulation: Difference between revisions

Content added Content deleted
No edit summary
(Added OCaml)
Line 3,053: Line 3,053:
4,8,12,16,20,60
4,8,12,16,20,60
</pre>
</pre>



=={{header|OCaml}}==
Using the '''csv''' module available in '''Opam''':

<lang ocaml>let list_add_last this lst =
List.rev (this :: (List.rev lst))

let () =
let csv = Csv.load "data.csv" in
let fields, data =
(List.hd csv,
List.tl csv)
in
let fields =
list_add_last "SUM" fields
in
let sums =
List.map (fun row ->
let tot = List.fold_left (fun tot this -> tot + int_of_string this) 0 row in
list_add_last (string_of_int tot) row
) data
in
Csv.output_all (Csv.to_channel stdout) (fields :: sums)</lang>

{{out}}
<pre>
$ opam install csv
$ ocaml -I $(ocamlfind query csv) csv.cma rc_csv.ml
C1,C2,C3,C4,C5,SUM
1,5,9,13,17,45
2,6,10,14,18,50
3,7,11,15,19,55
4,8,12,16,20,60
</pre>



=={{header|PARI/GP}}==
=={{header|PARI/GP}}==