CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
sorting hash

 



liven
Novice

Feb 25, 2008, 10:50 AM

Post #1 of 4 (187 views)
sorting hash Can't Post

I have a hash like this:



Code
$VAR1 = \{ 
'group_A' => {
'sub1' => 29,
'sub2' => 30
},
'group_c' => {
'sub1' => 7
},
'group_D' => {
'no-subgroup' => 2
},
'group_B' => {
'no-subgroup' => 4
},
'group_F' => {
'no-subgroup' => 8
},
'group_G' => {
'no-subgroup' => 6
},
'group_H' => {
'no-subgroup' => 31
}
}



This is built out of an array like this:

$excell_hash->{$group}{$sub_group}=$group_row;


What i am trying to do is sort the array by the keys $group and subgroup. And if sub stitute the $group_row value accordingly (lowest to highest).


Any ideas?


KevinR
Veteran


Feb 25, 2008, 1:13 PM

Post #2 of 4 (181 views)
Re: [liven] sorting hash [In reply to] Can't Post

Sorting by the $group key should be easy enough, also sorting by $sub_group should not be too hard but the rest of your question makes no sense:


Quote
And if sub stitute the $group_row value accordingly (lowest to highest).


Can you clarify and show what you have tried so far.
-------------------------------------------------


liven
Novice

Feb 25, 2008, 1:30 PM

Post #3 of 4 (178 views)
Re: [KevinR] sorting hash [In reply to] Can't Post

Kevin I took another approach...

Basicially I was trying to sort by the group and sub group, which is easy like you suggested.

But then I was also trying to sort by the value:


Code
$VAR1 = \{  
'group_A' => {
'sub1' => 29,
'sub2' => 30


that sub1 and sub2 pointed to....

In the above example the 29 and 30...

But after looking at the output that produced it was not what I needed.

I instead created an array from a combination of group plus sub group.

Sorted that array. Then used the index value out of that array to order a hash of arrays. I sure it is ugly, but it is accomplishing my goal.

Thanks for you help.


KevinR
Veteran


Feb 25, 2008, 1:37 PM

Post #4 of 4 (177 views)
Re: [liven] sorting hash [In reply to] Can't Post


Quote
I instead created an array from a combination of group plus sub group.

Sorted that array.


Excellent. Well done. Smile
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives