[Pdx-pm] for vs foreach question
Thomas Keller
kellert at ohsu.edu
Wed Dec 10 13:17:53 CST 2003
Basically, it is a matter of understanding what is $_ in the foreach
loop. I forgot that it would be the actual element of my arrays rather
than the index; whereas in the for loop I defined the indices
explicitly.
Sigh. But it doesn't hurt to rethink about the basics now and then.
Thanks all.
Tom
On Dec 10, 2003, at 11:00 AM, Jeff Zucker wrote:
> Thomas Keller wrote:
>
>> Hi all,
>> I thought the "foreach" keyword was a synonym for the "for" keyword.
>> So I'm perplexed why the following loops don't give the same output:
>
>
> First, I think if you add "each" to the "for" in the first loop, it
> will continue to work and if you take off "each" from the second loop,
> it will continue to not work. The use of "for" or "foreach" is not
> relevant to your problem.
>
> Here is the second loop written correctly with more legible variable
> names so you can see what's going on:
>
> my $table = $aref;
> for my $row (@$table) {
> foreach my $column (@$row) {
> print $column, " ";
> }
> print "\n";
> }
>
> Write back if that doesn't clarify things for you.
>
> --
> Jeff
>
>
More information about the Pdx-pm-list
mailing list