Skip to content

Instantly share code, notes, and snippets.

@ray1729
Created August 12, 2010 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ray1729/521160 to your computer and use it in GitHub Desktop.
Save ray1729/521160 to your computer and use it in GitHub Desktop.
Process common chunks of a sorted input stream
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
my ( $key, @values ) = next_record();
while ( defined $key ) {
my ( $next_key, @next_values ) = next_record();
if ( defined $next_key and $next_key eq $key ) {
push @values, @next_values;
}
else {
print join( "\t", $key, @values ) . "\n";
$key = $next_key;
@values = @next_values;
}
}
sub next_record {
defined( local $_ = <> ) or return;
chomp; split;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment