while (my $line = ) { # this sets $filerecord[0] to a file1 value my @filerecord = $line; for (@goodarray){ my $data = $_; # this sets $arrfield[0] only to a file2 value my @arrfields = $data; # this removes line ending from $arrfield[0] # which holds the file2 value chomp (@arrfields); # this trims $filedata[0] which refers # to $filerecord[0] the file1 value my $filedata =\@filerecord; ${$filedata}[0] =~ s/^\s+|\s+$//g; # this trims the file2 value $arrfields[0] =~ s/^\s+|\s+$|-//g; #$arrfields[0] =~ s/-//g; # this sets $string to trimmed file1 value my $string = ${$filedata}[0]; # this extract number from start of file1 value if ($string =~ /(^\d{7,8})/ ){ # $substr holds number from file1 my $substr = $1; # this matches file1 value to the number # extracted from file1 value # so will allways match if (index($string, $substr) !=-1){ #print "$string\n"; #last; } # this would work matching file1 value to file2 value if (index($string, $arrfields[0]) != -1){ print "$string\n"; last; } } } }