Perl Tutorials -Part 44- The ‘foreach’ loop

Posted on June 2, 2010. Filed under: Perl Tutorials |


#perl example of foreach loop -- array
@films = ('Fifth Element', 'La Haine', 'Alien');

foreach (@films)
{
      print $_;
      print "\n";
}

#perl example of foreach loop -- hash
%familyAges=
(
     'Susanne' => 24,
     'Josh' => 24,
     'Phiannon' => 2,
     'Tara' => 0
);

foreach (keys (%familyAges))
{
      print "$_ is $familyAges{$_} years old. \n";
}

Output:

1
2
3
4
5
6
7
Fifth Element
La Haine
Alien
Phiannon is 2 years old.
Josh is 24 years old.
Susanne is 24 years old.
Tara is 0 years old.
Advertisements

Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: