Re: [ph-perl] sorting hash values

Top Page
Author: Clemen Canaria
Date:  
To: ph-perl
Subject: Re: [ph-perl] sorting hash values
it worked! thanks!

Sherwin Daganato wrote:

>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
>
>
>


--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Clemen Canaria
Web Developer, Systems Development and Testing
Information Services
ccanaria@???

Global Sources (NASDAQ: GSOL)
Creating and facilitating global trade
between buyers and suppliers
www.globalsources.com

15 Floor Citibank Center Bldg.,
Paseo de Roxas, Makati, Philippines
Tel: (63) 2 8942762
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


_______________________________________________
ph-perl mailing list
ph-perl@???
http://lists.q-linux.com/mailman/listinfo/ph-perl