Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing a sorted array of hashes

Status
Not open for further replies.

kbhai94

Programmer
Mar 25, 2010
1
0
0
US
Code:
  use strict; 
        use warnings; 
        my @aoh =( 
         { 
            3 => 15, 
            4 => 8, 
            5 => 9, 
         }, 
         { 
            3 => 11, 
            4 => 25, 
            5 => 6, 
         }, 
        { 
            3 => 5, 
            4 => 18, 
            5 => 5, 
        }, 
        { 
            0 => 16, 
            1 => 11, 
            2 => 7, 
        }, 
        { 
            0 => 21, 
            1 => 13, 
            2 => 31, 
         }, 
        { 
            0 => 11, 
            1 => 14, 
            2 => 31, 
        }, 
        ); 
        #declaring a new array to store the sorted hashes 
        my @new; 
        print "\n-------------expected output------------\n"; 
        foreach my $href (@aoh) 
        { 
        #i want a new array of hashes where the hashes are sorted 
        my %newhash; 
        my @sorted_keys = sort {$href->{$b} <=> $href->{$a} || $b <=> $a} keys %$href; 
           foreach my $key (@sorted_keys) 
           { 
                 print "$key => $href->{$key}\n"; 
               $newhash{$key} = $href->{$key}; 
         } 
        print "\n"; 
        push(@new,\%newhash); 
        } 
        print "-----------output i am getting---------------\n"; 
        foreach my $ref(@new) 
        { 
         my @skeys = skeys %$ref; 
           foreach my $key (@skeys) 
           { 
               print "$key => $ref->{$key}\n" 
           } 
           print "\n"; 
        }
    
    
    
 The output of the program :
    
        -------------expected output------------ 
        3 => 15 
        5 => 9 
        4 => 8 
        
        4 => 25 
        3 => 11 
        5 => 6 
        
        4 => 18 
        5 => 5 
        3 => 5 
        
        0 => 16 
        1 => 11 
        2 => 7 
        
        2 => 31 
        0 => 21 
        1 => 13 
        
        2 => 31 
        1 => 14 
        0 => 11 
      
     
    
            -----------output i am getting---------------
        4 => 8
        3 => 15
        5 => 9
        
        4 => 25
        3 => 11
        5 => 6
        
        4 => 18
        3 => 5
        5 => 5
        
        1 => 11
        0 => 16
        2 => 7
        
        1 => 13
        0 => 21
        2 => 31
        
        1 => 14
        0 => 11
        2 => 31
Please tell me what am i doing wrong in storing the hashes into a new array.. how do i achieve what i want.. ? Thanks in advance...
 
Shouldn't this:

Code:
 my @skeys = skeys %$ref;

Be:

Code:
 my @skeys = s[red]ort [/red]keys %$ref;

Annihilannic.
 
Hashes don't maintain any kind of usable order. Using the data structure you have, you will need to sort the hashes just prior to printing them.

If you want to store a sorted list of keys, you could use an array of arrays similar to this:
Code:
my @order;
foreach my $hash (@aoh) {
	push @order, [sort {$hash->{$b} <=> $hash->{$a} || 
					$b <=> $a } keys %$hash];
}

for (my $i = 0; $i <= $#order; $i++) {
	foreach my $key (@{$order[$i]}) {
		print "$key => $aoh[$i]->{$key}\n";
	}
	print "\n";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top