http://www.perlmonks.org?node_id=1008475


in reply to How can one call the lowest value of an array by reference?

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:

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;

Prints:

2: Rat Cow 3: Bat 5: Cat 12: Dog

Note the use of the reverse lookup hash byValue to ease accessing the animals in sorted order.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: How can one call the lowest value of an array by reference?
by supriyoch_2008 (Monk) on Dec 12, 2012 at 10:37 UTC

    Hi GrandFather,

    Thanks for your code.

    With kind regards,