use strict; use warnings; my $filename2 = "file2.txt"; my %seen2; my @unique2; my @strings2; my @line2; open my $handle2, '<', $filename2 || die "$0: Can't open $filename2 for reading: $!"; while (my $line2 = <$handle2>) { my @strings2 = $line2 =~ m/(RC=[0-9][0-9][0-9][0-9])/; foreach my $s2 (@strings2) { foreach my $s2 ( @strings2 ) { next if $seen2{ $s2 }++; push @unique2, $s2; print "$line2\n"; } } }