Perl Programming/Keywords/foreach
< Perl Programming | Keywords
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]![](http://upload.wikimedia.org/wikipedia/commons/thumb/d/d6/Crystal_Clear_action_apply.png/20px-Crystal_Clear_action_apply.png)
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