while ( ($ip, $a) = each (%hosts)) { foreach $line (@final) { s/$hosts{$1}/$hosts{$2}/g; print FQDN $_; } }