Perl Programming/Keywords/foreach

Previous: for Keywords Next: fork

The foreach keyword

edit

foreach is the keyword used to start a foreach loop.

Syntax

edit
  foreach my $variable (@list) ;

Examples

edit
  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
Previous: for Keywords Next: fork