my @arrayOfVals; open(my $tmp, "<", "/Users/bioinformatics/Desktop/NC_001903.gff.txt") or die "Could not open $!"; while (<$tmp>) { chomp; next if /^#/; # discard header, unneeded and will interfere push(@arrayOfVals, $_); } close($tmp); my %hashOfKeys; open (my $arrVal, "<", "/Users/bioinformatics/Desktop/cp26_dff.txt") or die "Could not open $!"; while (<$arrVal>) { chomp; $hashOfKeys{$_} = 1; } close($arrVal); my %hash; for my $k (0 .. $#arrayOfVals -1) { if ($arrayOfVals[$k] =~ /.*;locus_tag=(.*)/) { next unless $hashOfKeys{$1}; my $key = $1; if ($arrayOfVals[$k+1] =~ /.*;Name=(.*);protein_/) { $hash{$key} = $1; } } } print Dumper(\%hash);