Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

by GrandFather (Sage)
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;


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 (Scribe) on Dec 12, 2012 at 10:37 UTC

    Hi GrandFather,

    Thanks for your code.

    With kind regards,

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1008475]
[RonW]: Cool, stevieb
[stevieb]: Not to mention, the bundle has a C# IDE, so I can finally rid myself of using Visual Studio (or just vi/vim) when I work on berrybrew
[RonW]: I continue to avoid C#. And successfully
[stevieb]: I inhereted C#, so I had to become familiar with it pretty quickly. I am considering taking a crack at re-writing berrybrew in C++
[stevieb]: I haven't yet done the reserach needed to see if it's possible though

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-02-20 21:18 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (302 votes). Check out past polls.