my $lastreport = ''; foreach $line (@sorted_data) { chomp $line; $line =~ /(.*?)\,(.*?)\,(.*)/; $first = $1; $second = $2; $last = $3; if ($last eq $second) { if($second eq $first) { print "$second really = $first\n"; } else { $lastreport .= "$last only = $first\n"; } } } print $lastreport;