开发者

How can I maintain a sorted hash in Perl?

 @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,
    },
    );

I want the hashes in each array index sorted in reverse order based on values..

@sorted = sort { ........... please fill this..........} @aoh;

expected output

@aoh =(
 {
    4 => 8,
    5 => 9,
    3 => 15,
 },
 {
    5 => 6,
    3 => 11,
    4 => 25,
},
{
    5 => 5,
    3 => 5,
    4 => 18,
},
{
     2 => 7,
     1 => 11,
     0 => 16,
},
{
    1 => 13,
    0 => 21,
    2 => 31,
 },
{
    0 => 11,
    1 => 14,
    2 => 31,
},
);

Please help.. Thanks in advance.开发者_高级运维. Stating my request again: I only want the hashes in each array index to be sorted by values.. i dont want the array to be sorted..


Perl hashes do not have order. You must either switch them to arrays, or sort them upon usage (e.g. when you need to iterate over that hash).

The first solution could look like:

@aoh =(
 [{ 4 => 8 }
, { 5 => 9 },
, { 3 => 15 }
 ],

...

Second solution would be:

foreach $subhash (@aoh) {
   foreach $sorted_key (sort { $subhash->{$a} <=> $subhash->{$b} } keys %$subhash) {
      # Do something with $subhash->{$sorted_key};
   }
}


Hashes do not have a specific order. You should probably use arrays to maintain order.


You can do it with Tie::Hash::Sorted. But I would rather think of reviewing my data structure. If you provided more information on what kind of data you are going to store in your structure, rather than just plain numbers, you could get a better answer for your real question.

#!/usr/bin/perl
use strict;
use warnings;

use Tie::Hash::Sorted;

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,
    },
);

my @sorted = map {
        tie my %h, 'Tie::Hash::Sorted',
            Hash => { %$_ },
            Sort_Routine => sub {
                    [ sort { $_[0]{$a} <=> $_[0]{$b} } keys %{$_[0]} ]
                };
        \%h;
    } @aoh;

# output data
foreach my $elem (@sorted) {
    print "elem\n";
    while (my ($k, $v) = each %$elem) {
        print "    $k => $v\n";
    }
}


Try this things....

foreach $hash ( @aoh)
    {
            my %new  = reverse (%{$hash});
            foreach ( sort {$a <=> $b } keys (%new ))
            {
                    print " $new{$_} :$_ \n ";
            }
            print "-------------\n";
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜