0

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.

  • 2
    [Why do hash keys have different order when printing?](https://stackoverflow.com/questions/30340027/why-do-hash-keys-have-different-order-when-printing) – mkHun Aug 07 '17 at 10:10
  • 1
    The "problem" isn't your `++` call. That's perfectly ok. But the call `keys %fruit_color;` returns the keys of the hash in some arbitrary order and you cannot rely on it to be the same between different script runs. From the [docs](http://perldoc.perl.org/functions/keys.html): _"Hash entries are returned in an apparently random order."_ – PerlDuck Aug 07 '17 at 10:12

2 Answers2

4

Problem is not in your increment. The problem is in hashes. In computer memory, hashes are not stored in same order what you have declared, For example

my @fruits = keys %fruit_color;
print @fruits;

If you run the script more than a time and monitor the output it will give the different output.

Hashes and arrays both are same but having some difference, which is setting the key (In array we called as index, In hashes we called as key).

Whatever name you want, you can put it for as key and store the value. But array you can access data by only the index value.

So that, arranging item in an array is important because you are accessing by its index value. But there is no need to arrange the item in hashes because you have set the key.

For more info and perldoc

mkHun
  • 5,891
  • 8
  • 38
  • 85
0

Almost every programming language has a similar ++ operator (and a corresponding -- operator). It's called pre-increment and post-increment. ++$foo works like { $foo += 1; return $foo; }, but ++$foo is the more interesting version. It increments $foo immediately, but returns the previous value. This makes a lot of simple math a lot shorter to write, because you can specify whether you want to get the value before or after the modification.

piojo
  • 6,351
  • 1
  • 26
  • 36