Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Sorting a hash by Key

Posted on 2000-03-20
8
Medium Priority
?
207 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 85

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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 85

Accepted Solution

by:
ozo earned 400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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

885 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