in reply to Separate duplicate and unique records
In this code the line:
may be generalized to slide any size window over a stream:($cur, $next) = ( $next, scalar( <IN>));
Also I like to setup such a queue going into a loop than clean up a queue when exiting a loop. It seems clearer to me but I can only call that a personal preference.@queue[0 .. 4 ] = ( @queue[ 1 .. 3], scalar( <IN> ));
This code seems to work.
#!/usr/bin/perl -T use strict; use warnings; my ( $input, $unique, $dupe) = qw( input unique dupes ); my $is_dupe; open( IN, $input ) or die "Can not open $input"; open( UNIQUE, ">", $unique ) or die "Can not open $unique"; open( DUPES, ">", $dupe ) or die "Can not open $dupe"; my $cur = <IN>; exit if not defined $cur; my $next = <IN>; no warnings "uninitialized"; while ( 1) { if ( $cur == $next) { print DUPES $cur; $is_dupe = 1; } else { if ( $is_dupe) { print DUPES $cur; } else { print UNIQUE $cur; } $is_dupe = 0; } ($cur, $next) = ( $next, scalar( <IN>)); last if not defined $cur; }
|
|---|