简体   繁体   English

如何从Perl中的哈希数组创建哈希哈希?

[英]How can I create a hash of hashes from an array of hashes in Perl?

I have an array of hashes, all with the same set of keys, eg: 我有一组哈希,都有相同的键组,例如:

my $aoa= [
 {NAME=>'Dave', AGE=>12, SEX=>'M', ID=>123456, NATIONALITY=>'Swedish'},
 {NAME=>'Susan', AGE=>36, SEX=>'F', ID=>543210, NATIONALITY=>'Swedish'},
 {NAME=>'Bart', AGE=>120, SEX=>'M', ID=>987654, NATIONALITY=>'British'},
]

I would like to write a subroutine that will convert this into a hash of hashes using a given key hierarchy: 我想编写一个子程序,它将使用给定的键层次结构将其转换为哈希哈希值:

my $key_hierarchy_a = ['SEX', 'NATIONALITY'];
aoh_to_hoh ($aoa, $key_hierarchy_a) = @_;
 ...
}

will return 将返回

{M=>
  {Swedish=>{{NAME=>'Dave', AGE=>12, ID=>123456}},
   British=>{{NAME=>'Bart', AGE=>120, ID=>987654}}}, 
 F=>
  {Swedish=>{{NAME=>'Susan', AGE=>36,  ID=>543210}}
}

Note this not only creates the correct key hierarchy but also remove the now redundant keys. 请注意,这不仅会创建正确的密钥层次结构,还会删除现在的冗余密钥。

I'm getting stuck at the point where I need to create the new, most inner hash in its correct hierarchical location. 我陷入困境,需要在正确的分层位置创建新的,最内部的哈希。

The problem is I don't know the "depth" (ie the number of keys). 问题是我不知道“深度”(即键的数量)。 If I has a constant number, I could do something like: 如果我有一个常数,我可以这样做:

%h{$inner_hash{$PRIMARY_KEY}}{$inner_hash{$SECONDARY_KEY}}{...} = filter_copy($inner_hash,[$PRIMARY_KEY,$SECONDARY_KEY])

so perhaps I can write a loop that will add one level at a time, remove that key from the hash, than add the remaining hash to the "current" location, but it's a bit cumbersome and also I'm not sure how to keep a 'location' in a hash of hashes... 所以也许我可以编写一个循环,一次添加一个级别,从散列中删除该键,而不是将剩余的散列添加到“当前”位置,但它有点麻烦,而且我不知道如何保持哈希散列中的“位置”......

use Data::Dumper;

my $aoa= [
 {NAME=>'Dave', AGE=>12, SEX=>'M', ID=>123456, NATIONALITY=>'Swedish'},
 {NAME=>'Susan', AGE=>36, SEX=>'F', ID=>543210, NATIONALITY=>'Swedish'},
 {NAME=>'Bart', AGE=>120, SEX=>'M', ID=>987654, NATIONALITY=>'British'},
];

sub aoh_to_hoh {
  my ($aoa, $key_hierarchy_a) = @_;
  my $result = {};
  my $last_key = $key_hierarchy_a->[-1];
  foreach my $orig_element (@$aoa) {
    my $cur = $result;
    # song and dance to clone an element
    my %element = %$orig_element;
    foreach my $key (@$key_hierarchy_a) {
      my $value = delete $element{$key};
      if ($key eq $last_key) {
        $cur->{$value} ||= [];
        push @{$cur->{$value}}, \%element;
      } else {
        $cur->{$value} ||= {};
        $cur = $cur->{$value};
      }
    }
  }
  return $result;
}

my $key_hierarchy_a = ['SEX', 'NATIONALITY'];
print Dumper(aoh_to_hoh($aoa, $key_hierarchy_a));

As per @FM's comment, you really want an extra array level in there. 根据@ FM的评论,你真的想要一个额外的数组级别。

The output: 输出:

$VAR1 = {
          'F' => {
                   'Swedish' => [
                                  {
                                    'ID' => 543210,
                                    'NAME' => 'Susan',
                                    'AGE' => 36
                                  }
                                ]
                 },
          'M' => {
                   'British' => [
                                  {
                                    'ID' => 987654,
                                    'NAME' => 'Bart',
                                    'AGE' => 120
                                  }
                                ],
                   'Swedish' => [
                                  {
                                    'ID' => 123456,
                                    'NAME' => 'Dave',
                                    'AGE' => 12
                                  }
                                ]
                 }
        };

EDIT: Oh, BTW - if anyone knows how to elegantly clone contents of a reference, please teach. 编辑:哦,BTW - 如果有人知道如何优雅地克隆参考的内容,请教。 Thanks! 谢谢!

EDIT EDIT: @FM helped. 编辑编辑:@FM帮助。 All better now :D 现在好多了:D

As you've experienced, writing code to create hash structures of arbitrary depth is a bit tricky. 正如您所经历的那样,编写代码以创建任意深度的哈希结构有点棘手。 And the code to access such structures is equally tricky. 访问这些结构的代码同样棘手。 Which makes one wonder: Do you really want to do this? 这让人不禁: 你真的想这么做吗?

A simpler approach might be to put the original information in a database. 一种更简单的方法可能是将原始信息放入数据库中。 As long as the keys you care about are indexed, the DB engine will be able to retrieve rows of interest very quickly: Give me all persons where SEX = female and NATIONALITY = Swedish . 只要您关心的密钥被编入索引,数据库引擎就能够非常快速地检索感兴趣的行: 给我所有SEX =女性和国籍=瑞典语的人 Now that sounds promising! 现在听起来很有希望!

You might also find this loosely related question of interest. 您可能还会发现这个松散相关的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM