simple hash question

hi,

I want to create a hash that stores multiple "values" for a given "key".

ie:
@value = (100);
$someHash{$someKey} = $value;

but then I want to add a second (and subsequent) value and maintain the previous values for the key. I am not sure of the syntax but something like:

push(@value, (542));
$someHash{$someKey} = $value;

so if I was to print $someHash{$someKey} I would get both 100 and 542.

I have tried serveral ways of doing it and I can't seem to get anything working :)

could you tell me how to set this up, the syntax for adding new values, and how I access a given element from the list of values for a particular key ?

thanks in advance
grimjbAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

prady_21Commented:

This should help
push(@{$somehash}{$somevalue}),$value);

and when printing


foreach $keys ( keys %somehash) {
 printf("%s: %s\n", $keys , join(', ', @{$hash{$keys}}));
}


0
prady_21Commented:
i am sorry , there is a mistake,
push(@{$somehash}{$somevalue}),$value);

should be
push(@{$somehash{$somevalue}},$value);
0
grimjbAuthor Commented:
I tried that but I got compliation errors on the push line. Here is what I wrote to test this method:

#!perl

%theHash=();

$value = 876;
$index = "elephant"

push(@{$theHash{$index}}, $value);
#push(@{$somehash{$somevalue}},$value);

foreach $k (keys %theHash)
{
     print $theHash{$k};
}

any ideas ?
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

prady_21Commented:
just cut and paste this code, and try
it is working absolutely fine in my system
if it dosent work do tell

#!/usr/bin/perl

$value1 = "something";
$value2 = "againsomething";

$index = "keyofhash";


push (@{$hash{$index}}, $value1);
push (@{$hash{$index}}, $value2);

foreach $keys (keys (%hash) ) {
  printf("%s:  %s\n", $keys, join(', ', @{$hash{$keys}}));
}
exit 0;


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prady_21Commented:
The problem with ur code was that a semi colon was missing at the end of $index = "elephant"
That was the error
0
prady_21Commented:
You also have to mention the path of perl
just do  

which perl

and you will know the directory where perl exists in your system
0
grimjbAuthor Commented:
That hit the spot :) I can't believe I missed a semi.

thanks for the help!!
0
grimjbAuthor Commented:
prady_21 was really good to put up with a perl newbie like me :) thanks again
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

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.