Home > Archive > PERL Beginners > October 2006 > Maximum value in Hash
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Maximum value in Hash
|
|
| Andrej Kastrin 2006-10-02, 3:58 am |
| Dear all,
I'm looking for simple (and fast) solution to extract maximum value from
a hash. I search over the Perl mailing lists, but I didn't find anything
usable.
Thanks in advance for any suggestion, Andrej
| |
| Wijaya Edward 2006-10-02, 3:58 am |
|
It depends what do you mean by maximum.
Assume you have numerical value as hash values.
You can do:
use List::Util qw(max)
my %hash = { foo => 3, bar=>2, qux=> 1};
my $max_val = max values %hash;
Hope that helps.
--
Regards,
Edward WIJAYA
________________________________
From: Andrej Kastrin [mailto:andrej.kastrin@siol.net]
Sent: Mon 10/2/2006 3:52 PM
To: beginners perl
Subject: Maximum value in Hash
Dear all,
I'm looking for simple (and fast) solution to extract maximum value from
a hash.
------------ Institute For Infocomm Research - Disclaimer -------------
This email is confidential and may be privileged. If you are not the intended recipient, please delete it and notify us immediately. Please do not copy or use it for any purpose, or disclose its contents to any other person. Thank you.
--------------------------------------------------------
| |
| John W. Krahn 2006-10-02, 3:58 am |
| Andrej Kastrin wrote:
> Dear all,
Hello,
> I'm looking for simple (and fast) solution to extract maximum value from
> a hash. I search over the Perl mailing lists, but I didn't find anything
> usable.
One way to do it:
my $max;
$max < $_ and $max = $_ for values %hash;
And another way:
my $max;
while ( my ( undef, $value ) = each %hash ) {
$max = $value if $max < $value;
}
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
| |
| Andrej Kastrin 2006-10-02, 3:58 am |
| John W. Krahn wrote:
> Andrej Kastrin wrote:
>
>
> Hello,
>
>
>
> One way to do it:
>
> my $max;
> $max < $_ and $max = $_ for values %hash;
>
>
> And another way:
>
> my $max;
> while ( my ( undef, $value ) = each %hash ) {
> $max = $value if $max < $value;
> }
>
>
>
> John
>
Thanks for your help, Andrej
|
|
|
|
|