CSV data manipulation: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 627:
3,7,11,300,19
4,8,12,16,400
</pre>
 
=={{header|C sharp}}==
<lang csharp>using System.IO;
using System.Linq;
 
namespace CSV_data_manipulation
{
class Program
{
static void Main()
{
var input = File.ReadAllLines("test_in.csv");
var output = input.Select((line, i) =>
{
if (i == 0)
return line + ",SUM";
var sum = line.Split(',').Select(int.Parse).Sum();
return line + "," + sum;
}).ToArray();
File.WriteAllLines("test_out.csv", output);
}
}
}
</lang>
{{out|Output (in <tt>test_out.csv</tt>)}}
<pre>
Column0,C2,C3,C4,C5,SUM
1,100,9,13,17,140
2,6,200,14,18,240
3,7,11,300,19,340
4,8,12,16,400,440
</pre>
 
Line 766 ⟶ 798:
3,7,11,300,19
4,8,12,16,400
</pre>
 
=={{header|C sharp}}==
<lang csharp>using System.IO;
using System.Linq;
 
namespace CSV_data_manipulation
{
class Program
{
static void Main()
{
var input = File.ReadAllLines("test_in.csv");
var output = input.Select((line, i) =>
{
if (i == 0)
return line + ",SUM";
var sum = line.Split(',').Select(int.Parse).Sum();
return line + "," + sum;
}).ToArray();
File.WriteAllLines("test_out.csv", output);
}
}
}
</lang>
{{out|Output (in <tt>test_out.csv</tt>)}}
<pre>
Column0,C2,C3,C4,C5,SUM
1,100,9,13,17,140
2,6,200,14,18,240
3,7,11,300,19,340
4,8,12,16,400,440
</pre>
 
Line 1,236:
</pre>
 
=={{Headerheader|Forth}}==
<lang forth>\ csvsum.fs Add a new column named SUM that contain sums from rows of CommaSeparatedValues
\ USAGE:
Line 1,294:
4,8,12,16,20,60.000</pre>
 
=={{Headerheader|Fortran}}==
=== Fortran 2003 ===
It's fairly easy to read arbitrary lines using allocatable character strings, available since Fortran 2003.
Line 2,184:
}
</lang>
 
 
=={{header|JavaScript}}==
Line 2,592 ⟶ 2,591:
4,8,12,16,20,60
</pre>
 
 
=={{header|M2000 Interpreter}}==
Line 2,644 ⟶ 2,642:
Checkit
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Mathematica's Import and Export functions support CSV files.
<lang mathematica>iCSV=Import["test.csv"]
->{{"C1","C2","C3","C4","C5"},{1,5,9,13,17},{2,6,10,14,18},{3,7,11,15,19},{4,8,12,16,20}}
iCSV[[1, 1]] = Column0;
iCSV[[2, 2]] = 100;
iCSV[[3, 3]] = 200;
iCSV[[4, 4]] = 300;
iCSV[[5, 5]] = 400;
iCSV[[2, 3]] = 60;
Export["test.csv",iCSV];</lang>
{{out}}
<pre>Column0,C2,C3,C4,C5
1,100,60,13,17
2,6,200,14,18
3,7,11,300,19
4,8,12,16,400</pre>
 
=={{header|Maple}}==
Line 2,696 ⟶ 2,676:
96
</lang>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Mathematica's Import and Export functions support CSV files.
<lang mathematica>iCSV=Import["test.csv"]
->{{"C1","C2","C3","C4","C5"},{1,5,9,13,17},{2,6,10,14,18},{3,7,11,15,19},{4,8,12,16,20}}
iCSV[[1, 1]] = Column0;
iCSV[[2, 2]] = 100;
iCSV[[3, 3]] = 200;
iCSV[[4, 4]] = 300;
iCSV[[5, 5]] = 400;
iCSV[[2, 3]] = 60;
Export["test.csv",iCSV];</lang>
{{out}}
<pre>Column0,C2,C3,C4,C5
1,100,60,13,17
2,6,200,14,18
3,7,11,300,19
4,8,12,16,400</pre>
 
=={{header|MATLAB}} / {{header|Octave}}==
Line 3,146 ⟶ 3,144:
# Print the output.
$csv->print(*STDOUT, $_) for \@header, @rows;</lang>
 
=={{header|Perl 6}}==
On the face of it this task is pretty simple. Especially given the sample CSV file and the total lack of specification of ''what'' changes to make to the file. Something like this would suffice.
<lang perl6>my $csvfile = './whatever.csv';
my $fh = open($csvfile, :r);
my @header = $fh.get.split(',');
my @csv = map {[.split(',')]>>.Num}, $fh.lines;
close $fh;
 
my $out = open($csvfile, :w);
$out.say((@header,'SUM').join(','));
$out.say((@$_, [+] @$_).join(',')) for @csv;
close $out;</lang>
But if your CSV file is at all complex you are better off using a CSV parsing module. (Complex meaning fields that contain commas, quotes, newlines, etc.)
<lang perl6>use Text::CSV;
my $csvfile = './whatever.csv';
my @csv = Text::CSV.parse-file($csvfile);
# modify(@csv); # do whatever;
csv-write-file( @csv, :file($csvfile) );</lang>
 
=={{header|Phix}}==
Line 3,333 ⟶ 3,312:
4,8,12,16,20,60
</pre>
 
=={{header|Prolog}}==
Add a "SUM" column. Output is as for Lua and is not repeated here.
 
The following uses SWI-Prolog's csv_read_file_row/3 in order to
demonstrate that it is not necessary to read more than a line at a time.
<lang Prolog>test :- augment('test.csv', 'test.out.csv').
 
% augment( +InFileName, +OutFileName)
augment(InFile, OutFile) :-
open(OutFile, write, OutStream),
( ( csv_read_file_row(InFile, Row, [line(Line)]),
% Row is of the form row( Item1, Item2, ....).
addrow(Row, Out),
csv_write_stream(OutStream, [Out], []),
fail
)
; close(OutStream)
).
 
% If the first item in a row is an integer, then append the sum;
% otherwise append 'SUM':
addrow( Term, NewTerm ) :-
Term =.. [F | List],
List = [X|_],
(integer(X) -> sum_list(List, Sum) ; Sum = 'SUM'),
append(List, [Sum], NewList),
NewTerm =.. [F | NewList].
</lang>
 
=={{header|PowerShell}}==
Line 3,407 ⟶ 3,357:
4 8 12 16 20 60
</pre>
 
=={{header|Prolog}}==
Add a "SUM" column. Output is as for Lua and is not repeated here.
 
The following uses SWI-Prolog's csv_read_file_row/3 in order to
demonstrate that it is not necessary to read more than a line at a time.
<lang Prolog>test :- augment('test.csv', 'test.out.csv').
 
% augment( +InFileName, +OutFileName)
augment(InFile, OutFile) :-
open(OutFile, write, OutStream),
( ( csv_read_file_row(InFile, Row, [line(Line)]),
% Row is of the form row( Item1, Item2, ....).
addrow(Row, Out),
csv_write_stream(OutStream, [Out], []),
fail
)
; close(OutStream)
).
 
% If the first item in a row is an integer, then append the sum;
% otherwise append 'SUM':
addrow( Term, NewTerm ) :-
Term =.. [F | List],
List = [X|_],
(integer(X) -> sum_list(List, Sum) ; Sum = 'SUM'),
append(List, [Sum], NewList),
NewTerm =.. [F | NewList].
</lang>
 
=={{header|PureBasic}}==
Line 3,617 ⟶ 3,596:
4,8,12,16,20,60
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
On the face of it this task is pretty simple. Especially given the sample CSV file and the total lack of specification of ''what'' changes to make to the file. Something like this would suffice.
<lang perl6>my $csvfile = './whatever.csv';
my $fh = open($csvfile, :r);
my @header = $fh.get.split(',');
my @csv = map {[.split(',')]>>.Num}, $fh.lines;
close $fh;
 
my $out = open($csvfile, :w);
$out.say((@header,'SUM').join(','));
$out.say((@$_, [+] @$_).join(',')) for @csv;
close $out;</lang>
But if your CSV file is at all complex you are better off using a CSV parsing module. (Complex meaning fields that contain commas, quotes, newlines, etc.)
<lang perl6>use Text::CSV;
my $csvfile = './whatever.csv';
my @csv = Text::CSV.parse-file($csvfile);
# modify(@csv); # do whatever;
csv-write-file( @csv, :file($csvfile) );</lang>
 
=={{header|Red}}==
Line 4,264 ⟶ 4,263:
4,8,12,116,20
</pre>
 
=={{header|Visual FoxPro}}==
<lang vfp>
10,333

edits