Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
38
Task/CSV-data-manipulation/Perl/csv-data-manipulation-1.pl
Normal file
38
Task/CSV-data-manipulation/Perl/csv-data-manipulation-1.pl
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/perl
|
||||
use warnings;
|
||||
use strict;
|
||||
|
||||
use List::Util 'sum';
|
||||
|
||||
my @header = split /,/, <>;
|
||||
# Remove the newline.
|
||||
chomp $header[-1];
|
||||
|
||||
my %column_number;
|
||||
for my $i (0 .. $#header) {
|
||||
$column_number{$header[$i]} = $i;
|
||||
}
|
||||
my @rows = map [ split /,/ ], <>;
|
||||
chomp $_->[-1] for @rows;
|
||||
|
||||
# Add 1 to the numbers in the 2nd column:
|
||||
$_->[1]++ for @rows;
|
||||
|
||||
# Add C1 into C4:
|
||||
$_->[ $column_number{C4} ] += $_->[ $column_number{C1} ] for @rows;
|
||||
|
||||
# Add sums to both rows and columns.
|
||||
push @header, 'Sum';
|
||||
$column_number{Sum} = $#header;
|
||||
|
||||
push $_, sum(@$_) for @rows;
|
||||
push @rows, [
|
||||
map {
|
||||
my $col = $_;
|
||||
sum(map $_->[ $column_number{$col} ], @rows);
|
||||
} @header
|
||||
];
|
||||
|
||||
# Print the output.
|
||||
print join(',' => @header), "\n";
|
||||
print join(',' => @$_), "\n" for @rows;
|
||||
25
Task/CSV-data-manipulation/Perl/csv-data-manipulation-2.pl
Normal file
25
Task/CSV-data-manipulation/Perl/csv-data-manipulation-2.pl
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/perl
|
||||
use warnings;
|
||||
use strict;
|
||||
|
||||
use Text::CSV;
|
||||
use List::Util 'sum';
|
||||
|
||||
my $csv = 'Text::CSV'->new({eol => "\n"})
|
||||
or die 'Cannot use CSV: ' . 'Text::CSV'->error_diag;
|
||||
|
||||
my $file = shift;
|
||||
my @rows;
|
||||
open my $FH, '<', $file or die "Cannot open $file: $!";
|
||||
my @header = @{ $csv->getline($FH) };
|
||||
while (my $row = $csv->getline($FH)) {
|
||||
push @rows, $row;
|
||||
}
|
||||
$csv->eof or $csv->error_diag;
|
||||
|
||||
#
|
||||
# The processing is the same.
|
||||
#
|
||||
|
||||
# Print the output.
|
||||
$csv->print(*STDOUT, $_) for \@header, @rows;
|
||||
Loading…
Add table
Add a link
Reference in a new issue