Re: [ph-perl] sorting hash values

Top Page
Author: Sherwin Daganato
Date:  
To: ph-perl
New-Topics: [ph-perl] char count
Subject: Re: [ph-perl] sorting hash values
On Thu, Jan 02, 2003 at 03:24:42PM +0800, Clemen Canaria wrote:
> $VAR = {
>          'record' => {
>                        'rec001' => {
>                                       'fname' => 'linus',
>                                       'lname' => 'torvalds',
>                                       'email' => 'linus@???',
>                                     },
>                        'rec002' => {
>                                       'fname' => 'allan',
>                                       'lname' => 'cox ',
>                                       'email' => 'allancox@???',
>                                     },
>                        'rec003' => {
>                                       'fname' => 'tigert',
>                                       'lname' => '',
>                                       'email' => 'tigert@???',
>                                     },
>                             }
>                  };

>
> how do i sort their fname's then display it using foreach or any other
> loop? hope you could help me.


Try this:

my @keys_sorted_by_fname =
  map { $_->[1] }
    sort { $a->[0] cmp $b->[0] }
      map { [ $VAR->{record}{$_}{fname}, $_ ] }
        keys %{ $VAR->{record} };



foreach my $key (@keys_sorted_by_fname) {
print <<EOT;
fname: $VAR->{record}{$key}{fname}
lname: $VAR->{record}{$key}{lname}
email: $VAR->{record}{$key}{email}
EOT
}

HTH

--
$_=q:; # SHERWIN #
70;72;69;6e;74;20;
27;4a;75;73;74;20;
61;6e;6f;74;68;65;
72;20;50;65;72;6c;
20;6e;6f;76;69;63;
65;27;:;;s=~?(..);
?=pack q$C$,hex$1;
;;;=egg;;;;eval;;;
_______________________________________________
ph-perl mailing list
ph-perl@???
http://lists.q-linux.com/mailman/listinfo/ph-perl