http://qs1969.pair.com?node_id=603361


in reply to Read file line by line and check equal lines

You may be looking for a Perl solution, so I'll give you feedback. You may be learning Perl, or may not be running on a Unix platform.

First a comment about lexical variables and use strict;. Get yourself into the habit of declaring your variables with my, and only declaring them in the narrowest scope that you need them. A Super search on "use strict" will give you many answers that explain why this is a good idea.

Secondly, your test for evenness of the line count is the wrong way to do things. Each time you get an odd number of singular lines, it will throw out the evenness check.

Here's my stab:

use strict; use warnings; my $prev; while (<DATA>) { chomp; # Note that $prev will be undef only on the first time round. my $curr = $_; if (!defined $prev) { $prev = $curr; next; } if ($curr ne $prev) { print "$prev\n"; $prev = $curr; } else { undef $prev; } } print "$prev\n" if defined $prev; __DATA__ a1a a1a b1b c1c c1c d1d d1d e1e f1f g1g g1g h1h h1h i1i j1j

Note that you don't deal with, or say whay you want to happen when you get three or more identical lines in the input.

--
Apprentice wetware hacker