multiple variables as value to a hash

how do I add two variables as a "value" to single key in Perl hash
saibskAsked:
Who is Participating?
 
Adam314Commented:
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
 
mkatmonkeyCommented:
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
 
TintinCommented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
ozoCommented:
reading your question literally
$hash{$key} = $var1+$var2;
but maybe you mean
$hash{$var1,$var2}
could you clarify?
0
 
saibskAuthor Commented:
I mean $hash{key} = value1, value2
0
 
ozoCommented:
that's equivalent to
($hash{key}=value1),value2
or did you mean
$hash{key} = [value1, value2];
or
$hash{key} = value1. value2
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.