my ( @results, %fields ); while(<>){ # Strip all NULL characters out of the current line s/\0//g; if (/(?[1] cmp $b->[1] } @results; open OUT, ">results.txt" or die $!; foreach( @final ) { # Print only one occurence of each statement my $fieldNames = $_->[1]; if ( ! exists $fields{ $fieldNames } ) { $fields{ $fieldNames } = 1; print OUT @{ $_ } } } close OUT;