按散列Perl的散列值散列进行排序 [英] Sort by value hash of hash of hashes Perl

查看:138
本文介绍了按散列Perl的散列值散列进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下的散列结构:

  KeyA => {
Key1 => {
Key4 => 4
Key5 => 9
Key6 => 10
}
Key2 => {
Key7 => 5
Key8 => 9
}
}
KeyB => {
Key3 => {
Key9 => 6
Key10 => 3




$ b我需要打印出穿过散列结构和遍历结束时的值,以便按值排序。
例如,对于上述散列结构,我需要打印:

  KeyB Key3 Key10 3 
KeyA Key1 Key4 4
KeyA Key2 Key7 5
KeyB Key3 Key9 6
KeyA Key2 Key8 9
KeyA Key1 Key5 9
KeyA Key1 Key6 10

有没有更高效的方法来解决这个问题?

解决方案

使用递归解决方案的任意嵌套深度的嵌套数据结构。您递归地构建一个包含路径和值的目标数组,然后对该数组进行排序。

 使用警告; 
使用strict;

子路径{
my($ data,$ cur_path,$ dest)= @_;
if(ref $ data eq'HASH'){
foreach my $ key(keys%data){
paths($ data-> {$ key},[@ $ cur_path ,$ key],$ dest);
}
} else {
push @ $ dest,[$ cur_path,$ data];
}
}

my $ data = {
KeyA => {
Key1 => {Key4 => 4,Key5 => 9,Key6 => 10},
Key2 => {Key7 => 5,Key8 => 9}
},
KeyB => {Key3 => {Key9 => 6,Key10 => 3}}
};

my $ dest = [];
路径($ data,[],$ dest);

foreach my $ result(sort {$ a-> [1]< => $ b-> [1]} @ $ dest){
print join(' ',@ {$ result-> [0]},$ result-> [1]),\\\
;
}


I have a hash structure similar to the following:

KeyA => {
         Key1 => {
                   Key4 => 4
                   Key5 => 9
                   Key6 => 10
                 }
         Key2 => {
                   Key7 => 5
                   Key8 => 9
                 }
        }
KeyB => {
         Key3 => {
                   Key9 => 6
                   Key10 => 3
                 }
        }

I need to print out the traversal path through the hash structure and the value at the end of the traversal, such that this is ordered by value. For example, for the above hash structure I need to print:

KeyB Key3 Key10 3
KeyA Key1 Key4  4
KeyA Key2 Key7  5
KeyB Key3 Key9  6
KeyA Key2 Key8  9
KeyA Key1 Key5  9
KeyA Key1 Key6  10

Currently, to solve this I am traversing the hash structure using nested foreach loops, and creating a flattened hash by inserting an element with key equal to the traversal path (e.g. "KeyA Key3 Key10") and value equal to the value at the end of the traversal path (e.g. 3), then doing another foreach loop which sorts the flattened hash by value.

Is there a more efficient way to do this?

解决方案

You can also solve this problem for a nested data structure of arbitrary nesting depth using a recursive solution. You recursively build up a destination array containing paths and values, and then sort that array.

use warnings;
use strict;

sub paths {
    my ($data, $cur_path, $dest) = @_; 
    if (ref $data eq 'HASH') {
        foreach my $key (keys %$data) {
            paths($data->{$key}, [@$cur_path, $key], $dest);
        }   
    } else {
        push @$dest, [$cur_path, $data];
    }   
}

my $data = {
    KeyA => {
        Key1 => { Key4 => 4, Key5 => 9, Key6 => 10 },
        Key2 => { Key7 => 5, Key8 => 9 }
    },
    KeyB => { Key3 => { Key9 => 6, Key10 => 3 } }
};

my $dest = []; 
paths($data, [], $dest);

foreach my $result (sort { $a->[1] <=> $b->[1] } @$dest) {
    print join(' ', @{$result->[0]}, $result->[1]), "\n";
}

这篇关于按散列Perl的散列值散列进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆