Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Max numbers of element in the values of an HoH

by secret (Beadle)
on Dec 16, 2005 at 14:25 UTC ( #517246=perlquestion: print w/replies, xml ) Need Help??

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

Hello Dear Monks,

I have a hash like this one :

my %result= ( '2005/12/15-01:00' => { 'c4t13d0' => '1751 1.0 67.2', 'c5t8d0' => '314 1.0 9.5', }, '2005/12/14-22:20' => { 'c4t12d0' => '1688 1.0 58.9', 'c5t12d0' => '1701 1.0 65.5 125 12.2 12 ', 'c5t8d0' => '545 1.0 12.2', 'c4t8d0' => '791 1.0 18.8' }, '2005/12/12-22:20' => { 'c4t12d0' => '1690 1.0 58.9', 'c5t12d0' => '1703 1.0 65.5', 'c5t8d0' => '566 1.0 13.3 12.36 321', 'c4t8d0' => '840 1.0 22.2' }, ) ;

What i want is the maximum number of numbers in the string ( which is the value of the hash ) .
I could record this number when i initialy load the hash (parsing a file) or loop over keys, but i though of using values, which proved interesting . Here is what i came up with :

my @list ; push @list, values %$_ for values %result ; my ($seen) = sort {$b<=>$a} map { my @tmp = split " ", $_ ; scalar @tmp } @list ; print $seen ;
I wonder if there are better / more efficient / more readable ways ?

Thanks for your advice

Replies are listed 'Best First'.
Re: Max numbers of element in the values of an HoH
by Ido (Hermit) on Dec 16, 2005 at 14:52 UTC
    If you only need the maximum (and you're not golfing), I see no reason to sort. You could use something like:
    my $max=0; for(map values %$_,values %result){ my $n=split; $max=$n if $n>$max; } print $max;
    HTH
      neat, since it doesn't need the @list nor the @tmp either !
      thx

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://517246]
Approved by Happy-the-monk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2020-09-26 09:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If at first I donít succeed, I Ö










    Results (141 votes). Check out past polls.

    Notices?