Solved

multiple variables as value to a hash

Posted on 2007-11-15
6
570 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
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 250 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
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 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 250 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

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 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

13 Experts available now in Live!

Get 1:1 Help Now