This code produces the output you want:
$ perl -Mstrict -Mwarnings -E '
use List::MoreUtils qw{pairwise}; $a || $b || 1;
use Data::Dumper;
my @array = qw( 111 222 333 444 555 888 );
my @array2 = qw( acct1 acct2 acct3 acct4 acct5 acct8 );
my $data = [ pairwise { +{ $a => $b } } @array, @array2 ];
print Dumper $data;
'
$VAR1 = [
{
'111' => 'acct1'
},
{
'222' => 'acct2'
},
{
'333' => 'acct3'
},
{
'444' => 'acct4'
},
{
'555' => 'acct5'
},
{
'888' => 'acct8'
}
];
Notes on my code:
-
I've excluded the duplicate acct4 (noted earlier by jeffa).
-
See List::MoreUtils for details on pairwise().
-
$a || $b || 1; stops "used only once" warnings for both $a and $b. Another way to do this would be with no warnings 'once'; but that's less specific and could potentially mask other, unrelated problems (see perllexwarn).
-
The + in front of { $a => $b } indicates to perl that this is a hashref — without it, perl interprets { $a => $b } as an anonymous block.
Notes on your code:
-
In scalar context, @some_array evaluates to the number of elements in @some_array, so my $vals_1 = @{$got1}; will result in $vals_1 having a value of 6 (ditto for $vals_2). This would have resulted in messages about trying to use 6 as an array reference. (It would have been better if you had shown your error messages rather than just saying "... having issues ...".)
-
The output you say you want ($VAR2 = [ ... ];) is an arrayref; however, your code is populating hashes. Take a look at the Perl Data Structures Cookbook.
If the output that you indicated you wanted is not correct, please clarify what you do want.
-
In the context of your code, where you use print Dumper %data; you probably want print Dumper \%data; (ditto for print Dumper %hash_of_data;). Take a look at Data::Dumper.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.