Perl Programming/Keywords/foreach
Appearance
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