in reply to Getting Max Value from Different Hashes

How about using List::Util ?

my $max_val = max map { values %$_ } $hash_w, $hash_x, $hash_y, $hash_ +z;

Replies are listed 'Best First'.
Re^2: Getting Max Value from Different Hashes
by diotalevi (Canon) on Aug 01, 2005 at 15:03 UTC

    Corion, your method is straightforward but suffers because it makes a copy of every value at once. You could still use max() but only have one hash copy present at a time just by switching to reduce().

    my @hashes = ( $hash_w, $hash_x, $hash_y, $hash_z ); my $max = max( values %{shift @hashes } ); $max = reduce { max( $_[0], values %{$_[1]} ) } @hashes;
      I hope that the OP was not seriuos about the efficiency issue - this is why such an optimisation over 4 values seems weird to me :)

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.
        That isn't over four values, that's over four collections each of unknown size. I was seriously considering adding this other method just because the collection size was unknown. This moves from collection-at-a-time to element-at-a-time.

        my @collections = ...; my $first_collection = shift @collections; keys %$first_collection; my ( undef, $max ) = each %$first_collection; while ( my ( undef, $elt ) = each %$first_collection ) { if ( $elt > $max ) { $max = $elt; } } for my $collection ( @collections ) { keys %$collection; while ( my ( undef, $elt ) = each %$collection ) { if ( $elt > $max ) { $max = $elt; } } }