I am new to perl. I cannot understand the output of the following codes.
my %fruit_color = (apple => "red", banana => "yellow", grape => "purple");
my @fruits = keys %fruit_color;
my @colors = values %fruit_color;
print "The color of apple is ", $fruit_color{"apple"}, "\n";
$cnt = 0;
while ($cnt < @fruits) {
print $fruits[$cnt ++], " ";
}
print "\n";
$cnt = 0;
while ($cnt < @colors) {
print $colors[$cnt ++], " ";
}
The output is pasted here:
The color of apple is red
grape banana apple
purple yellow red
However, if i changed my code like this:
my %fruit_color = (apple => "red", banana => "yellow", grape => "purple");
my @fruits = keys %fruit_color;
my @colors = values %fruit_color;
print "The color of apple is ", $fruit_color{"apple"}, "\n";
$cnt = 0;
while ($cnt < @fruits) {
print $fruits[$cnt], " "; # DIFF HERE !
$cnt ++;
}
print "\n";
$cnt = 0;
while ($cnt < @colors) {
print $colors[$cnt ++], " ";
}
The output will be:
The color of apple is red
apple grape banana
red purple yellow
I can't understand the difference between these examples, especially why the change of the first while loop will effect the second one. Could anybody tell me why the output is in a reversed order? thanks a lot.