[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

multiple variables as value to a hash

Posted on 2007-11-15
6
Medium Priority
?
579 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 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 85

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 85

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

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

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 …
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…
Six Sigma Control Plans
Suggested Courses

834 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