Solved

Multidimentional HASH

Posted on 2011-09-19
7
253 Views
Last Modified: 2012-08-13
I am New to perl,Please help me.
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
Comment
Question by:SWAYAM4U
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 3

Expert Comment

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

Author Comment

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

Expert Comment

by:woepwobin
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 28

Expert Comment

by:FishMonger
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;

Open in new window

0
 
LVL 9

Assisted Solution

by:parparov
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);

Open in new window

0
 
LVL 9

Accepted Solution

by:
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];
  }
}

Open in new window

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;
  }
}

Open in new window


0
 

Author Closing Comment

by:SWAYAM4U
ID: 36565190
Thanks a lot to everybody
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl CPAN issue 3 97
Perl troubleshooting error. Base64 bootstrap parameter 5 63
Edit Odbc.ini using perl module 2 76
what are these modules in perl script 4 93
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now