Solved

Multidimentional HASH

Posted on 2011-09-19
257 Views
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 ?

0
Question by:SWAYAM4U
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 2
• 2
• 2
• +1

LVL 3

Expert Comment

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

Author Comment

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

LVL 3

Expert Comment

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

LVL 28

Expert Comment

ID: 36560750
``````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

LVL 9

Assisted Solution

parparov earned 500 total points
ID: 36560853
You can also:
Install module List::MoreUtils;
``````use List::MoreUtils qw(mesh);
%hash = mesh(@array_a, @array_b);
``````
0

LVL 9

Accepted Solution

parparov earned 500 total points
ID: 36561785
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

Author Closing Comment

ID: 36565190
Thanks a lot to everybody
0

Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses
Course of the Month7 days, 18 hours left to enroll