?
Solved

multiple variables as value to a hash

Posted on 2007-11-15
6
Medium Priority
?
576 Views
Last Modified: 2012-06-21
how do I add two variables as a "value" to single key in Perl hash
0
Comment
Question by:saibsk
[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
  • Learn & ask questions
6 Comments
 
LVL 5

Expert Comment

by:mkatmonkey
ID: 20291126
You can generate some sort of key based on those two variables or you can create a hash of hashes.  It's up to you to decide which approach is best based on your application.

Usually the best way is to generate a key by concatenating the two values together (if they're strings):

Simple key generator:

my $key = join('|',@values);

Open in new window

0
 
LVL 48

Expert Comment

by:Tintin
ID: 20291882
Depends a little on what you mean.

Did you mean

$var1='abc';
$var2='def';
$hash{'key'}=$var1$var2;

or did you mean

$var1='abc';
$var2='def';
$hash{$var1$var2}='value';


0
 
LVL 39

Accepted Solution

by:
Adam314 earned 750 total points
ID: 20291927
Or you could create a hash of array:

my %hash;
$hash{key} = [$var1, $var2];

You then access them as:
$hash{key}->[0]    #this is $var1
$hash{key}->[1]    #this is $var2
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 84

Expert Comment

by:ozo
ID: 20292584
reading your question literally
$hash{$key} = $var1+$var2;
but maybe you mean
$hash{$var1,$var2}
could you clarify?
0
 

Author Comment

by:saibsk
ID: 20292971
I mean $hash{key} = value1, value2
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 750 total points
ID: 20293058
that's equivalent to
($hash{key}=value1),value2
or did you mean
$hash{key} = [value1, value2];
or
$hash{key} = value1. value2
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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 …
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

765 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