http://qs321.pair.com?node_id=437005


in reply to A simple foreach question

I believe the reason is this: You are receiving the input in list context. Therefore when you enter '2 3 4' it is being recieved as a string and the array has only one element. Now, when you iterate through your 1 element array, you are trying to use it as a number, but since the one element '2 3 4' is a string, only the 2 is being used. It would be better to do something like this:

#!/usr/bin/perl use strict; my @names = qw/ fred betty barney dino wilma peblles bamm-bamm /; chomp( my $list = <STDIN>); my @list = split(" ", $list); foreach (@list) { print " $names[ $_ - 1]\n"; } exit;
davidj