1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 my %colors = (
7 apple => 'red',
8 orange => 'orange',
9 lemon => 'yellow',
10 lime => 'green',
11 grape => 'purple'
12 );
13
14
15 #
16 # Using a while loop & each
17 while( my( $fruit, $color ) = each %colors ) {
18 print "A $fruit is $color.\n";
19 }
20
21 print "\n";
22
23 #
24 # Foreach keys looping
25 foreach my $fruit (sort keys %colors) {
26 my $prefix = 'A';
27 if( $fruit =~ /^[aeiou]/i ) {
28 $prefix .= 'n';
29 }
30
31 my $color = $colors{$fruit};
32 my $end = '.';
33 if( $fruit eq $color ) {
34 $end = ', duh!';
35 }
36
37 print "$prefix $fruit is $color$end\n";
38 }
syntax highlighted by Code2HTML, v. 0.9.1