Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Sort and Access Math::Currency Objects

by PerlSufi (Friar)
on Aug 15, 2014 at 16:10 UTC ( [id://1097581]=note: print w/replies, xml ) Need Help??


in reply to Re: Sort and Access Math::Currency Objects
in thread Sort and Access Array of Hash

Hi kennethk, my apologies!
I actually do not care about the value of the Math::Currency object. I actually need the key for $lowest. I have updated my post

Replies are listed 'Best First'.
Re^3: Sort and Access Math::Currency Objects
by kennethk (Abbot) on Aug 15, 2014 at 16:15 UTC
    If you want the key that corresponds the the lowest value of a hash, instead of code like
    my $lowest = min values $products_and_prices;
    you'll want something like
    my ($lowest) = sort {$products_and_prices->{$a} <=> $products_and_pric +es->{$b}} keys $products_and_prices;
    The parentheses create list context on the assignment, so that lowest gets assigned to the first list element, which is the item that was smallest. Of course, you could write something more efficient, and I haven't tested the above because you still haven't posted useful example code.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      kennethk, I was getting 'Not a Hash Reference' with that sort method, I have updated my code with a sample data structure. Sorry again. UPDATE: With your method slightly modified, I got it:
      my ($lowest) = sort {$products_and_prices->{$a} <=> $products_and_pric +es->{$b}} keys @{$products_and_prices}[0];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-03-28 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found