Solved

Sorting a hash by Key

Posted on 2000-03-20
8
201 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

774 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