I got interrupted or I'd have put this up earlier. This is much like thelonius's solution. I post it only because it shows the queue idea much more clearly.

In this code the line:

($cur, $next) = ( $next, scalar( <IN>));
may be generalized to slide any size window over a stream:
@queue[0 .. 4 ] = ( @queue[ 1 .. 3], 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.

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; }

In reply to Re: Separate duplicate and unique records by rir
in thread Separate duplicate and unique records by wsee

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.