# Multidimentional HASH

I have 2 Arrays
@ARRAYA=(A,B,C,D,E,A);
@ARRAYB=(1,2,3,4,5,6);

I want to Create a hash out of these two array like

%Hash=('A'=>1 ,6 ,' B' =>2,'C'=>3,'D'=>4,'E'=>5) ;

after creating this hash how take out the value of A ?

###### Who is Participating?

Commented:
Oh you needed a list of values if the key repeats.

Then the script should be:
``````my @arrayA = qw(A B C D E A);
my @arrayB = qw(1 2 3 4 5 6);

my %hash;
for my \$aA (@arrayA) {
last unless @arrayB;
if(defined(\$hash{\$aA}) {
push(@{\$hash{\$aA}}, shift @arrayB);
}
else {
\$hash{\$aA} = [shift @arrayB];
}
}
``````
if you want a consistent data structure.
However if you insist that if there is a single value, it would be a scalar, then use this code:
``````my @arrayA = qw(A B C D E A);
my @arrayB = qw(1 2 3 4 5 6);

my %hash;
for my \$aA (@arrayA) {
last unless @arrayB;
if(defined(\$hash{\$aA}) {
\$hash{\$aA} = [ \$hash{\$aA}] unless ref \$hash{\$aA};
push(@{\$hash{\$aA}}, shift @arrayB);
}
else {
\$hash{\$aA} = shift @arrayB;
}
}
``````

0

Commented:
What exactly do you mean by 'take out the value of A' ? What is your expected result?
0

Author Commented:
After creating the hash
The KEy is A and it's value is 1 and 6 .
0

Commented:
And what is your expected result? What do you want the value to be? 1, or 6, or 7, or "1,6"?
0

Commented:
``````use Data::Dumper;

my @arrayA = qw(A B C D E A);
my @arrayB = qw(1 2 3 4 5 6);

my %hash;
@hash{@arrayA} = @arrayB;

print Dumper \%hash;
``````
0

Commented:
You can also:
Install module List::MoreUtils;
``````use List::MoreUtils qw(mesh);
%hash = mesh(@array_a, @array_b);
``````
0

Author Commented:
Thanks a lot to everybody
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.