Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Sort and Access Array of Hash

by PerlSufi (Friar)
on Aug 15, 2014 at 15:36 UTC ( [id://1097576]=perlquestion: print w/replies, xml ) Need Help??

PerlSufi has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,
Solved: I actually need the key of the variable $lowest below:
use strict; use warnings; use List::Util qw(min); use Data::Dumper; my $products_and_prices; my $sellable_products = [ { one => 1, two => 2, }, ]; for my $prod ( @{$sellable_products} ) { push @{$products_and_prices}, { $prod->name => $prod->price, }; } my $lowest = min values $products_and_prices; print Dumper($lowest);
Any insight is greatly appreciated.

Replies are listed 'Best First'.
Re: Sort and Access Array of Hash
by AppleFritter (Vicar) on Aug 15, 2014 at 18:10 UTC

    The code in your post doesn't even compile, so given the lack of a clear description of your problem, I'll have to guess, but -- I take it that what you want to do is, given a hash that maps products (i.e. their names) to their prices, find the product with the lowest price?

    If so, use values and keys, along with grep and List::Util's ever-useful min:

    #!/usr/bin/perl use strict; use warnings; use List::Util qw/min/; my $sellable_products = { one => 1, two => 2, }; my $lowest_price = min values %$sellable_products; my @cheapest_products = grep { $sellable_products->{$_} == $lowest_pri +ce } keys %$sellable_products;
Re: Sort and Access Math::Currency Objects
by kennethk (Abbot) on Aug 15, 2014 at 16:07 UTC
    Not to be difficult, but at no point does your posted code invoke Math::Currency, so I have no method for replicating your issue. Surely you've read How do I post a question effectively?.

    The error message means that the method on the line that issues that error is being called on a value that is not a blessed reference. Most often when I get this error, it's because a subroutine unexpectedly returned an undef/empty list.

    So unless you post a self-contained example that emits the cited error, it's all just guessing.


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

      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
        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.

Re: Sort and Access Array of Hash
by Anonymous Monk on Aug 17, 2014 at 12:13 UTC
    If what you need is the lowest price, you don't have to sort anything:   as you are walking through the list, set a $lowest_price variable to the lowest price you find. Initially let that variable be undef which means that any price wins; thereafter, a price lower than this one wins. By the time you have finished loading the data structure you also know what is the lowest (and highest) price in it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1097576]
Front-paged by GotToBTru
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2024-04-23 16:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found