在定义新数据结构时使用引用连接数组

时间:2014-08-05 01:29:16

标签: arrays perl

首先发帖给我:)

我不确定这是否可能,但我想知道。

鉴于以下代码......

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my %email_addresses = (
    'fred' => [
        '"Fred Blogs" <fred.blogs@domain.com>',
        '"Fred Blogs" <fred.blogs@hotmail.com>'
    ],
    'jane' => [
        '"Jane Smith" <jane.smith@domain.com>',
        '"Jane Smith" <jane.smith@hotmail.com>',
        '"Jane Smith" <jane.smith@somwhere.com>'
    ],
    'tom' => [
        '"Tom Jones" <tom.jones@domain.com>'
    ]
);

my %recipients = (
    'success' => [
        $email_addresses{'fred'},
        $email_addresses{'jane'}
    ],
    'failure' => [
        $email_addresses{'tom'}
    ]
);

print Data::Dumper->Dump([\%recipients], ['recipients']);

输出

$recipients = {
                'success' => [
                               [
                                 '"Fred Blogs" <fred.blogs@domain.com>',
                                 '"Fred Blogs" <fred@hotmail.com>'
                               ],
                               [
                                 '"Jane Smith" <jane.smith@domain.com>',
                                 '"Jane Smith" <jane.smith@hotmail.com>',
                                 '"Jane Smith" <jane.smith@somwhere.com>'
                               ]
                             ],
                'failure' => [
                               [
                                 '"Tom Jones" <tom.jones@domain.com>'
                               ]
                             ]
              };

结果是$recipients{'success'}$recipients{'failure'}都是二维数组,但这不是我想要的。我希望它们是一维数组。

也就是说,对于$recipients{'success'},我希望将Jane的电子邮件地址列表附加到Fred的电子邮件地址列表中,从而生成包含5个元素的一维数组。类似地,$recipients{'failure'}将是仅包含1个元素的一维数组。

所以,我希望它看起来像这样......

$recipients = {
                'success' => [
                               '"Fred Blogs" <fred.blogs@domain.com>',
                               '"Fred Blogs" <fred@hotmail.com>',
                               '"Jane Smith" <jane.smith@domain.com>',
                               '"Jane Smith" <jane.smith@hotmail.com>',
                               '"Jane Smith" <jane.smith@somwhere.com>'
                             ],
                'failure' => [
                               '"Tom Jones" <tom.jones@domain.com>'
                             ]
              };

现在,抓住了 ......我想知道这是否可以在我定义my %recipients时完成 - 所有这些都在一个陈述中。

我知道在使用其他语句定义之后我可以通过编程方式实现我想要的功能,但我很想知道它是否可以在一个语句中完成。我尝试了(),[],{}和解除引用的各种组合,但没有任何效果。

谢谢大家。

1 个答案:

答案 0 :(得分:4)

您基本上想要展平数组引用列表。是的,这很容易实现。

我建议您只使用地图并传递要翻译的键列表,如下所示:

use strict;
use warnings;

use Data::Dumper;

my %email_addresses = (
    'fred' => [
        '"Fred Blogs" <fred.blogs@domain.com>',
        '"Fred Blogs" <fred.blogs@hotmail.com>',
    ],
    'jane' => [
        '"Jane Smith" <jane.smith@domain.com>',
        '"Jane Smith" <jane.smith@hotmail.com>',
        '"Jane Smith" <jane.smith@somwhere.com>',
    ],
    'tom' => [
        '"Tom Jones" <tom.jones@domain.com>',
    ]
);

my %recipients = (
    'success' => [map @{$email_addresses{$_}}, qw(fred jane)],
    'failure' => [map @{$email_addresses{$_}}, qw(tom)],
);

print Data::Dumper->Dump([\%recipients], ['recipients']);

输出:

$recipients = {
                'success' => [
                               '"Fred Blogs" <fred.blogs@domain.com>',
                               '"Fred Blogs" <fred.blogs@hotmail.com>',
                               '"Jane Smith" <jane.smith@domain.com>',
                               '"Jane Smith" <jane.smith@hotmail.com>',
                               '"Jane Smith" <jane.smith@somwhere.com>'
                             ],
                'failure' => [
                               '"Tom Jones" <tom.jones@domain.com>'
                             ]
              };