my %seen = (); my $tmp ... $seen{$_}++; if ($_ ne $tmp && $seen{$tmp} == 1){ print $_; $tmp =$_; }elsif ($_ eq $tmp){ print $_ if $seen{$_} == 2; }