从哈希数组中删除数组引用

时间:2014-08-26 10:52:10

标签: perl

我有多级哈希数组,我想让它成为单个哈希数组。但是我无法做到这一点。

实际数组

[
  [
    { 'country' => 'India' },
    { 'country' => 'India' }
  ],
  [
    { 'country' => 'India' },
    { 'country' => 'India' }
  ],
  [
    { 'country' => 'India' },
    { 'country' => 'India' }
  ]
];

需要数组

[
  { 'country' => 'India' },
  { 'country' => 'India' },
  { 'country' => 'India' },
  { 'country' => 'India' },
  { 'country' => 'India' },
  { 'country' => 'India' }
];

1 个答案:

答案 0 :(得分:3)

您必须取消引用每个子数组并将其展平为hashref元素列表

my $a1 = [
    [ { country => "India" }, { country => "India" } ],
    [ { country => "India" }, { country => "India" } ],
    [ { country => "India" }, { country => "India" } ],
];

# Flatten the AoA
my $a2 = [ map @$_, @$a1 ];

use Data::Dump;
dd $a2;

输出:

[
  { country => "India" },
  { country => "India" },
  { country => "India" },
  { country => "India" },
  { country => "India" },
  { country => "India" },
]