4

有一个问题可以准确地解释我在这里想要什么:how to merge 2 deep hashes in perl

但是,那里的答案似乎对我不起作用(使用Merge模块的建议)。

我有两个像这样的哈希:

$VAR1 = {
          '57494' => {
                       'name' => 'John Smith',
                       'age' => '9',
                       'height' => '120'
                     },
          '57495' => {
                       'name' => 'Amy Pond',
                       'age' => '17',
                       'height' => '168'
                     }
        }
};
$VAR1 = {
          '57494' => {
                       'name_address' => 'Peter Smith',
                       'address' => '5 Cambridge Road',
                       'post_code' => 'CR5 0FS'
                     }
        }
};

如果我使用Hash::Merge%c = {%a,%b}我每次都得到的格式:

$VAR1 = '57494';
$VAR2 = {
          'name_address' => 'Peter Smith',
          'address' => '5 Cambridge Road',
          'post_code' => 'CR5 0FS'
        };

(所以它基本上用第二个数据覆盖了第一个数据并弄乱了键)当我想要的时候:

$VAR1 = {
          '57494' => {
                       'name' => 'John Smith',
                       'age' => '9',
                       'height' => '120'
                       'name_address' => 'Peter Smith',
                       'address' => '5 Cambridge Road',
                       'post_code' => 'CR5 0FS'
                     },
          '57495' => {
                       'name' => 'Amy Pond',
                       'age' => '17',
                       'height' => '168'
                     }
        }
};

因此,当键相同时,数据合并在一起,否则新键只是附加到末尾。我希望这是有道理的。也许我使用错误Merge或需要“手动”将它们添加到循环中,但我花了太多时间考虑它,无论如何!

编辑:我如何使用 Merge 来查看我是否在做一些愚蠢的事情:

我有:

use Hash::Merge qw( merge );

...hash data above as %hash1 and %hash2...

my %combined_hash = %{ merge( %hash1,%hash2 ) };
print Dumper(%combined_hash);
4

2 回答 2

7

如果我用引用来做,它就像一个魅力。

use strict; use warnings;
use Data::Dumper;
use Hash::Merge qw(merge);
my $h1 = {
  '57494' => {
    'name'   => 'John Smith',
    'age'    => '9',
    'height' => '120'
  },
  '57495' => {
    'name'   => 'Amy Pond',
    'age'    => '17',
    'height' => '168'
  }
};

my $h2 = {
  '57494' => {
    'name_address' => 'Peter Smith',
    'address'      => '5 Cambridge Road',
    'post_code'    => 'CR5 0FS'
  }
};

my $h3 = merge( $h1, $h2 );
print Dumper $h3;

输出:

$VAR1 = {
      '57495' => {
                   'name' => 'Amy Pond',
                   'age' => '17',
                   'height' => '168'
                 },
      '57494' => {
                   'name_address' => 'Peter Smith',
                   'name' => 'John Smith',
                   'post_code' => 'CR5 0FS',
                   'address' => '5 Cambridge Road',
                   'height' => '120',
                   'age' => '9'
                 }
    };

但是,如果我使用散列而不是散列引用来执行此操作,则它不会:

my %hash1 = (
  '57494' => {
    'name'   => 'John Smith',
    'age'    => '9',
    'height' => '120'
  },
  '57495' => {
    'name'   => 'Amy Pond',
    'age'    => '17',
    'height' => '168'
  }
);

my %hash2 = (
  '57494' => {
    'name_address' => 'Peter Smith',
    'address'      => '5 Cambridge Road',
    'post_code'    => 'CR5 0FS'
  }
);

my %h3 = merge( %hash1, %hash2 );
print Dumper \%h3;

__END__
$VAR1 = {
  '57495' => undef
};

那是因为mergefrom Hash::Merge只能获取引用,但您传递的是哈希值。此外,您需要在标量上下文中调用它。

像这样尝试:

#                             +--------+--- references
#   ,-- SCALAR context        |        |
my $combined_hash = %{ merge( \%hash1, \%hash2 ) };
print Dumper($combined_hash);
于 2012-11-06T12:52:07.673 回答
1
for my $key (keys %fromhash) {
    if(not exists $tohash{$key}) {
        $tohash{$key} = {};
    }
    for my $subkey (keys %{$fromhash{$key}}) {
        ${$tohash{$key}}{$subkey} = ${$fromhash{$key}}{$subkey};
    }
}

或多或少的牙套取决于我上次喝的咖啡好不好。

Python 肯定更适合这种事情,因为它不会让你考虑引用:

for key in fromdict:
    if key not in todict:
        todict[key] = {}
    todict[key] = dict(fromdict[key].items() + todict[key].items())

或者如果todictdefaultdict(在读取和分配时创建键):

for key in fromdict:
    todict[key] = dict(dict(fromdict[key]).items() + dict(todict[key]).items())
于 2012-11-06T12:40:11.657 回答