newperl has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I am learning perl and hit a snag! I was hoping someone could help me with this really simple question. I am trying to make a program that takes in a list of numbers as input, and each number represents the corresponding name in my @name array. So if I put in 2 3 4, i should get betty barney dino as my output. But right now I only get fred as my output. It does not run through the entire list! Why is that? Did I code something wrong? Thanks
@names = qw/ fred betty barney dino wilma peblles bamm-bamm /; chomp(@list = <STDIN>); foreach (@list) { print "$names[ $_ - 1 ]\n"; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: A simple foreach question
by davido (Cardinal) on Mar 06, 2005 at 06:44 UTC | |
Re: A simple foreach question
by rlb3 (Deacon) on Mar 06, 2005 at 06:43 UTC | |
by betterworld (Curate) on Mar 06, 2005 at 16:55 UTC | |
Re: A simple foreach question
by chas (Priest) on Mar 06, 2005 at 06:43 UTC | |
Re: A simple foreach question
by runrig (Abbot) on Mar 06, 2005 at 06:49 UTC | |
Re: A simple foreach question
by calin (Deacon) on Mar 06, 2005 at 11:23 UTC | |
Re: A simple foreach question
by davidj (Priest) on Mar 06, 2005 at 06:53 UTC | |
Re: A simple foreach question
by gopalr (Priest) on Mar 06, 2005 at 06:53 UTC |
Back to
Seekers of Perl Wisdom