Skip to content

Instantly share code, notes, and snippets.

@mpapec
Created June 27, 2014 20:56
Show Gist options
  • Save mpapec/847669e027df33046fac to your computer and use it in GitHub Desktop.
Save mpapec/847669e027df33046fac to your computer and use it in GitHub Desktop.
SO jaypal
perl -F, -lane'
BEGIN {
$x = pop;
## Create array of arrays from start and end ranges
## $range = ( [0,0] , [1,10] ... )
(undef, @range)= map { chomp; [split /,/] } <>;
@ARGV = $x;
}
## Skip the first line
next if $. ==1;
## Create hash of hash
## $line = '[0,0]' => { "count" => counts , "sum4" => sum_of_col4 , "sum5" => sum_of_col5 }
for (@range) {
if ($F[3] >= $_->[0] && $F[3] <= $_->[1]) {
$line{"@$_"}{"count"}++;
$line{"@$_"}{"sum4"} +=$F[3];
$line{"@$_"}{"sum5"} +=$F[4];
}
}
}{
print "StartRange,EndRange,Count,Sum-4,Sum-5";
print join ",", @$_,
$line{"@$_"}{"count"} //"NotFound",
$line{"@$_"}{"sum4"} //"NotFound",
$line{"@$_"}{"sum5"} //"NotFound"
for @range
' slab input
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment