Reputation: 111
I am new to perl. I need to understand how can I map one array (as keys) to another (as values) to result in a hash using foreach loop:
@one = ("A", "B", "C");
@two = ("a", "b", "c");
I wrote the following code but it does not work when I slice the hash??
%hash;
foreach $i (one) {
print $i, "=>" , $ii = shift @two, "\n"
}
Upvotes: 6
Views: 1621
Reputation: 52336
Assuming the answer to my question in the comment is "yes", here's a couple of approaches.
Given:
my @one = qw/A B C/;
my @two = qw/1 2 3/;
Using hash slices:
my %hash;
@hash{@one} = @two;
Using the List::MoreUtils module from CPAN:
use List::MoreUtils qw/zip/;
my %hash = zip @one, @two;
Upvotes: 14