use Modern::Perl; # Process the second file only once my @alleles; { open my $secondfile, '<', './secondfile.txt' or die "Could not open second file. $!"; while (<$secondfile>) { chomp; my ($minor, $major) = split; push @alleles, { "$minor$minor" => 0, "$minor$major" => 1, "$major$minor" => 1, "$major$major" => 2, '00' => -1, } } } #Process the first file open my $firstfile, '<', './firstfile.txt' or die "Could not open first file. $!"; while (<$firstfile>) { chomp; say "Working on $_"; my @snps = split; my $index; my @results; while (@snps) { my $marker = shift(@snps) . shift(@snps); push @results, ($alleles[$index])->{$marker}; $index++; } say join ',', @results; }