< Perl Programming < Keywords

The foreach keyword

foreach is the keyword used to start a foreach loop.

Syntax

  foreach my $variable (@list) ;

Examples

The code
use 5.10.0;

%favorite = (joe => 'red', sam => 'blue', walter => 'black');

%list = %favorite;

say "%favorite = ";
foreach my $element (%favorite) {
  say $element;
}
returns the following:
%favorite =
walter
black
joe
red
sam
blue

See also

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.