foreach (qw(0 3 8 20)) { my ($word2) = split(/ /, $lines2[$_]); my ($word3) = split(/ /, $lines3[$_]); print "$lines1[$_]:$word2:$word3\n"; }