Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: How can one call the lowest value of an array by reference?

by GrandFather (Cardinal)
on Dec 12, 2012 at 10:05 UTC ( #1008475=note: print w/ replies, xml ) Need Help??


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


Comment on Re: How can one call the lowest value of an array by reference?
Select or Download Code
Re^2: How can one call the lowest value of an array by reference?
by supriyoch_2008 (Scribe) on Dec 12, 2012 at 10:37 UTC

    Hi GrandFather,

    Thanks for your code.

    With kind regards,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008475]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (20)
As of 2014-07-11 17:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (232 votes), past polls