Solved

Sorting a hash by Key

Posted on 2000-03-20
8
203 Views
Last Modified: 2010-03-05
Okay, some of you may have read my other question regarding sorting the output of an SQL statement in order to print a snippet of JavaScript properly. Well, I've figured some things out... and I am getting some good output now. All I need is one more step, and I'm on my way.

Basically, I need to sort a hash by the value of the $key (NOT by the $value itself).

My hash is called %parent_children and it contains this right now:

 1 - ARRAY(0x8172558)
11 - ARRAY(0x8201aec)
 2 - ARRAY(0x8172570)
 3 - ARRAY(0x8172564)
15 - ARRAY(0x8201b1c)
-1 - ARRAY(0x81dfef8)

Yes, the hash contains a key with a pointer to an array. I need to sort it so that it becomes:

-1 - ARRAY(0x81dfef8)
 1 - ARRAY(0x8172558)
 2 - ARRAY(0x8172570)
 3 - ARRAY(0x8172564)
11 - ARRAY(0x8201aec)
15 - ARRAY(0x8201b1c)

What's the best way to go about acheiving this?

-Mike K.
0
Comment
Question by:lunaboy
  • 3
  • 3
  • 2
8 Comments
 
LVL 5

Expert Comment

by:PC_User321
ID: 2638905
You can't sort a hash, but you can extract things in key order.

Do you mean that your hash contains:
 1 =>ARRAY(0x8172558),
11 =>ARRAY(0x8201aec),
 2 =>ARRAY(0x8172570),
 3 =>ARRAY(0x8172564),
15 =>ARRAY(0x8201b1c),
-1 =>ARRAY(0x81dfef8),

?
0
 
LVL 84

Expert Comment

by:ozo
ID: 2638919
for( sort{$a<=>$b} keys %parent_children ){
   printf "%2d - %s\n",$_,$parent_children{$_};
}
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2638928
If the hash is as I indicate above (i.e. keys are 1, 11, 2, etc) then you can access them in order like this:

foreach $Entry (sort keys %parent_children) {
   print "$parent_children{$Entry}\n";
}
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:lunaboy
ID: 2638932
> From: PC_User321
>
> You can't sort a hash, but you can extract things in key order.
>
> Do you mean that your hash contains:
>  1 =>ARRAY(0x8172558),
> 11 =>ARRAY(0x8201aec),
>  2 =>ARRAY(0x8172570),
>  3 =>ARRAY(0x8172564),
> 15 =>ARRAY(0x8201b1c),
> -1 =>ARRAY(0x81dfef8),
>
> ?


Yes, that is what it contains... and I want it to return (print) in the order I listed in my original post.

-Mike K.
0
 
LVL 1

Author Comment

by:lunaboy
ID: 2638947
ozo,

I tried your code above, but I must be more clear:

This is the code I'm using to print my data to the screen:

    my $arr_count=0;
    foreach $key (keys(%parent_children))
    {
        my $i=0;
        print "var array_$arr_count = new Array(";
        foreach(@{$parent_children{$key}})
        {
            unless($i==0){print ",";}
            print "new Array(\"$catid_name{$_}\",$_)";
            $i++;
        }
        print ")\;<BR>\n";
        $arr_count++;
    }

How can I print that in the same manner that you helped sort the print %parent_children?

-Mike K.
0
 
LVL 5

Expert Comment

by:PC_User321
ID: 2638977
Add 'sort' :-

foreach $key (sort keys(%parent_children))
   
0
 
LVL 84

Accepted Solution

by:
ozo earned 100 total points
ID: 2639008
foreach $key ( sort {$a<=>$b} keys(%parent_children) ){
    print "var array_$arr_count = new Array(";
    print join",",map{qq(new Array("$catid_name{$_}",$_))} @{$parent_children{$key}};
    print ");<BR>\n";
    $arr_count++;
}
0
 
LVL 1

Author Comment

by:lunaboy
ID: 2640860
Thanks ozo, worked like a charm.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question