my @input = split(/,/,$_); my $file_out= pop @input; ... foreach ... ... $_=~ s/<<>>/$input[$1]/ge;