Created
September 17, 2011 13:16
-
-
Save ptica/1223924 to your computer and use it in GitHub Desktop.
aggregate for mantos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
$encoding = 'cp1250'; | |
open FILE, "<:encoding($encoding)", "mantos.csv" or die $!; | |
open RESULT, ">:encoding($encoding)", "result.csv" or die $!; | |
my %aggregates = (); | |
foreach my $line (<FILE>) { | |
# get rid of newlines and other whitespace | |
$line =~ s/^\s+//; | |
$line =~ s/\s+$//; | |
# split line | |
my @fields = split(/;/, $line); | |
my $town = $fields[0]; | |
my $district = $fields[1]; | |
my $permanent = $fields[3]; | |
my $temporary = $fields[4]; | |
#skip first line | |
if ($town eq 'Obec') { next; } | |
# sum | |
$aggregates{"$town;$district"}->{'permanent_total'} += $permanent; | |
$aggregates{"$town;$district"}->{'temporary_total'} += $temporary; | |
} | |
# output header | |
print RESULT "obec;okres;perm;temp\n"; | |
# output data | |
while ( ($town_id, $info) = each %aggregates ) { | |
my $permanent = $info->{'permanent_total'}; | |
my $temporary = $info->{'temporary_total'}; | |
print RESULT "$town_id;$permanent;$temporary\n" | |
} | |
close(FILE); | |
close(RESULT); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment