Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Joining arrays using references when defining a new data structure

Tags:

arrays

perl

First post here for me :)

I'm not sure if this is even possible, but I would like to find out.

Given the following code...

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;

my %email_addresses = (
    'fred' => [
        '"Fred Blogs" <[email protected]>',
        '"Fred Blogs" <[email protected]>'
    ],
    'jane' => [
        '"Jane Smith" <[email protected]>',
        '"Jane Smith" <[email protected]>',
        '"Jane Smith" <[email protected]>'
    ],
    'tom' => [
        '"Tom Jones" <[email protected]>'
    ]
);

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

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

The output is

$recipients = {
                'success' => [
                               [
                                 '"Fred Blogs" <[email protected]>',
                                 '"Fred Blogs" <[email protected]>'
                               ],
                               [
                                 '"Jane Smith" <[email protected]>',
                                 '"Jane Smith" <[email protected]>',
                                 '"Jane Smith" <[email protected]>'
                               ]
                             ],
                'failure' => [
                               [
                                 '"Tom Jones" <[email protected]>'
                               ]
                             ]
              };

The result is that both $recipients{'success'} and $recipients{'failure'} are two dimensional arrays, however this is not what I want. I would like them to be one dimensional arrays.

That is, for $recipients{'success'}, I want the list of Jane's email addresses to be appended to the list of Fred's email addresses resulting in a one dimensional array containing 5 elements. Similarly, $recipients{'failure'} would be a one dimensional array containing only 1 element.

So, I want it to look like this...

$recipients = {
                'success' => [
                               '"Fred Blogs" <[email protected]>',
                               '"Fred Blogs" <[email protected]>',
                               '"Jane Smith" <[email protected]>',
                               '"Jane Smith" <[email protected]>',
                               '"Jane Smith" <[email protected]>'
                             ],
                'failure' => [
                               '"Tom Jones" <[email protected]>'
                             ]
              };

Now here's the catch... I want to know if this can be done at the point where I define my %recipients - all in one statement.

I know I can achieve what I want programmatically after the definition using additional statements, but I'm curious to know if it can be done all in one. I've tried various combinations of (), [], {} and dereferencing but nothing has worked.

Thanks all.

like image 213
protogen Avatar asked Apr 07 '26 19:04

protogen


1 Answers

You're essentially wanting to flatten a list of array references. Yes, that is easily accomplished.

I would advise that you just use map and pass a list of keys that you want to translate like so:

use strict;
use warnings;

use Data::Dumper;

my %email_addresses = (
    'fred' => [
        '"Fred Blogs" <[email protected]>',
        '"Fred Blogs" <[email protected]>',
    ],
    'jane' => [
        '"Jane Smith" <[email protected]>',
        '"Jane Smith" <[email protected]>',
        '"Jane Smith" <[email protected]>',
    ],
    'tom' => [
        '"Tom Jones" <[email protected]>',
    ]
);

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

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

Outputs:

$recipients = {
                'success' => [
                               '"Fred Blogs" <[email protected]>',
                               '"Fred Blogs" <[email protected]>',
                               '"Jane Smith" <[email protected]>',
                               '"Jane Smith" <[email protected]>',
                               '"Jane Smith" <[email protected]>'
                             ],
                'failure' => [
                               '"Tom Jones" <[email protected]>'
                             ]
              };
like image 154
Miller Avatar answered Apr 10 '26 08:04

Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!