in reply to Printing a Hash Slice problem

this should do the trick:
use strict; use warnings; my $HoA_ref = {'GAT' => [2, ['ttt',-3,1],['ttc',-3,3],['ccc',-1,2]], 'AAA' => [13,['aaa',-1,2],['atg',-2,2]], 'TTT' => [11,['tta',-2,1],['atc',-3,3]] }; sub print_zHoA { my ($HoA) = shift; my $mc = (sort {$$HoA{$a}[0] <=> $$HoA{$b}[0]} keys %$HoA)[-1]; print "$mc $$HoA{$mc}[0]:\n"; for my $i ( 1 .. $#{$$HoA{$mc}} ) { print join( ',', @{$$HoA{$mc}[$i]} ), "\n"; } } print_zHoA($HoA_ref);

Replies are listed 'Best First'.
Re^2: Printing a Hash Slice problem
by monkfan (Curate) on Mar 02, 2005 at 07:28 UTC
    Dear Taumarill,

    Thanks for answering. Just a question regarding your code above:
    my $mc = (sort {$$HoA{$a}[0] <=> $$HoA{$b}[0]} keys %$HoA)[-1];
    1. What does '-1' means here?
    2. Why '=' sign? Basically what does the statement above means?
    How does it work?

    Regards,
    Edward

      [-1] is a negative array index, which lets you count from the other end of an array. $array[-1] is the same as [reverse @array]->[0] or $array[$#array]

      <=> is a comparison operator for numbers, similar to cmp for strings. (see perlop or the sort documentation)