Perl Programming/Keywords/foreach

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Previous: for Keywords Next: fork

The foreach keyword[edit | edit source]

foreach is the keyword used to start a foreach loop.

Syntax[edit | edit source]

  foreach my $variable (@list) ;

Examples[edit | edit source]

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[edit | edit source]

Previous: for Keywords Next: fork