That doesn't exactly solve the stated problem, does it? As I read it, the OP only wants those lines that only appear one time in the input. What you've given is a way to display a given line from the input at most one time.
Here's a solution (untested, so there's probably boundary problems) that only needs to keep at most 3 lines in memory under the constraint that the lines are already sorted.
#!/usr/bin/perl
use strict; use warnings;
my ($p1, $p2);
while(<DATA>) {
next unless $p1 and $p2;
if ($p2 eq $p1) {$p2 = $p1 = undef; redo; }
if ($p2 ne $p1) { print $p2; next; }
} continue { $p2 = $p1; $p1 = $_; }
__DATA__
a1a
a1a
b1b
c1c
c1c
d1d
d1d
e1e
f1f
g1g
g1g
h1h
h1h
i1i
j1j
k1k
k1k
k1k
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|