From: "Chas Owens" <[EMAIL PROTECTED]>
> On 4/18/07, yitzle <[EMAIL PROTECTED]> wrote:
> > I got an array of hashes so I am using a foreach (@arr) loop to access
> > the hashes.
> > How do I go about looping through the hash's keys/values? I was
> > thinking of another foreach, but then the $_ gets a bit screwed up...
> >
> > Do I need to do this ?
> > foreach(@arr) {
> >   %hash = %{$_};
> >   foreach (keys %hash) {
> >     print "$_ => $hash{$_}\n";
> >   }
> > }
> 
> foreach is dead, long live for.

William is dead, long live Bill?

foreach and for are two names for the same thing and just as you can 
call someone both William and Bill you can use foreach and for 
interchangeably.

foreach(my $i = 0; $i < $whatever; $i++)
for(my $i = 0; $i < $whatever; $i++)

for my $x (@array)
foreach my $x (@array)

for (@array)
foreach (@array)

No difference to the computer. Use whichever reads best!

I would myself use "for" for the C-style loops and if I use the loop 
as a way to create an alias 

  for ($data->{$key}[1]{whatever}{booo}) {
    s/.../.../g;
    s/.../.../g;
    if (/.../) {
      $_ = "$3-$2-$1"
    }
  }

or

  for my $text ($data->{$key}[1]{whatever}{booo}) {
    $text =~ s/.../.../g;
    $text = foolify($text, $sumfin);
  }

and "foreach" is I need to loop through a collection (array, list, 
keys or values of a hash, ...). Though I don't think I'm 100% 
consistent in this.

Jenda
===== [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to