# reading in each query string (file1) into an array while($line1= ){ chomp($line1); push (@barcode,$line1); } foreach $code(@barcode){ my $filename = $code; open(OUT, ">$filename") || die "$!\n"; for my $data(keys %idhash){ # I have stored each of the query strings in a hash. The value of the hash contains the target strings. The keys are 1,2,3... my $value = $idhash{$data}; chomp($code); if($value =~ m/^$code/) # HOW DO I WRITE A REGULAR EXPRESSION HERE TO ALLOW ALL THE 6 COMBINATIONS TO BE MATCHED PER QUERY STRING. { print "$idhash{data}\n"; # where the query string matches the target string print value of the hash } } }