Solved

Sorting a hash by Key

Posted on 2000-03-20
8
197 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
Comment Utility
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
Comment Utility
for( sort{$a<=>$b} keys %parent_children ){
   printf "%2d - %s\n",$_,$parent_children{$_};
}
0
 
LVL 5

Expert Comment

by:PC_User321
Comment Utility
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
 
LVL 1

Author Comment

by:lunaboy
Comment Utility
> 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:lunaboy
Comment Utility
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
Comment Utility
Add 'sort' :-

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

Accepted Solution

by:
ozo earned 100 total points
Comment Utility
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
Comment Utility
Thanks ozo, worked like a charm.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now