in reply to A simple foreach question

Your program works, but with the following restrictions:

Keep in mind that the elements of @list in your progam are lines, not necessarily numbers. Here's another approach that is more flexible with input format. You can separate numbers with commas, spaces etc. Also, input is processed ASAP (after each line, buffering permitting) :

my @names = qw/ fred betty barney dino wilma peblles bamm-bamm /; while(<STDIN>) { print $names[$_ - 1], "\n" for grep {$_ <= @names} m/\d+/g; }