Solved

Sorting a hash by Key

Posted on 2000-03-20
8
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

636 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