in reply to Sorting HoHoA based on Length of Second Key

print "$_ # Length ", length( ( keys %{ $HoHoA{ $_ } } )[ 0 ] ), "\n" for map $_->[ 0 ], sort { $a->[ 1 ] <=> $b->[ 1 ] || $a->[ 2 ] <=> $b->[ 2 ] } map [ $_, length( ( keys %{ $HoHoA{ $_ } } )[ 0 ] ), substr( $_, 3 ) + ], keys %HoHoA; __END__ set2 # Length 3 set3 # Length 3 set4 # Length 4 set1 # Length 7
If you have no preference on the ordering of ties, then the ST part can be a bit simpler:
map $_->[ 0 ], sort { $a->[ 1 ] <=> $b->[ 1 ] } map [ $_, length( ( keys %{ $HoHoA{ $_ } } )[ 0 ] ) ], keys %HoHoA;
These solutions assume that all the secondary hashes contain at least one key.

the lowliest monk

Replies are listed 'Best First'.
Re^2: Sorting HoHoA based on Length of Second Key
by neversaint (Deacon) on Jul 27, 2005 at 06:19 UTC
    These solutions assume that all the secondary hashes contain at least one key.
    It's me again sir,

    Sorry to disturb you.
    how can I modify your snippet if there happen to be a 'primary' key that has no value, for example there is additional:
    'set5' =>{},
    Just like in my recent posting.
    I can always turn off warning, but it is still troubling. Hope to hear from you again.


    ---
    neversaint and everlastingly indebted.......

      Just change the expression

      ( keys %{ $HoHoA{ $_ } } )
      to
      ( keys %{ $HoHoA{ $_ } }, '' )
      That way the resulting list will always have at least one element (of length 0).

      the lowliest monk