note
GrandFather
<p>Trying to maintain parallel arrays in that fashion if fraught with peril. Instead use a data structure where you can treat each data pair as an entity. One way to do that would be to use OO and generate an object for each animal. For the current sample a simple hash will suffice. Consider:</p>
<c>
use strict;
use warnings;
my %animals = (
Cat => 5,
Bat => 3,
Cow => 2,
Dog => 12,
Rat => 2
);
my %byValue;
push @{$byValue{$animals{$_}}}, $_ for keys %animals;
my @ordered = sort {$a <=> $b} keys %byValue;
print "$_: @{$byValue{$_}}\n" for @ordered;
</c>
<p>Prints:</p>
<c>
2: Rat Cow
3: Bat
5: Cat
12: Dog
</c>
<p>Note the use of the reverse lookup hash byValue to ease accessing the animals in sorted order.</p>
<div class="pmsig"><div class="pmsig-461912">
True laziness is hard work
</div></div>
1008463
1008463