Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

multiple variables as value to a hash

Posted on 2007-11-15
6
Medium Priority
?
577 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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

610 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