Skip to content

Instantly share code, notes, and snippets.

@ptica
Created September 17, 2011 13:16
Show Gist options
  • Save ptica/1223924 to your computer and use it in GitHub Desktop.
Save ptica/1223924 to your computer and use it in GitHub Desktop.
aggregate for mantos
#!/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