in reply to A simple foreach question

Hello,

I think your problem is that <STDIN> is not returning an list. It's putting all the values in to $list[0]. I made two changes to your code and it seams to work.

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

Hope this helps.

Replies are listed 'Best First'.
Re^2: A simple foreach question
by betterworld (Curate) on Mar 06, 2005 at 16:55 UTC
    I think your problem is that <STDIN> is not returning an list.
    This is not exactly true. <STDIN> does return a list because it is called in list context. The list are the lines of input. For further details, see the other comments.