?
Solved

simple hash question

Posted on 2003-03-27
8
Medium Priority
?
140 Views
Last Modified: 2010-03-05
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
0
Comment
Question by:grimjb
[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
  • 5
  • 3
8 Comments
 
LVL 3

Expert Comment

by:prady_21
ID: 8222926

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

and when printing


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


0
 
LVL 3

Expert Comment

by:prady_21
ID: 8222932
i am sorry , there is a mistake,
push(@{$somehash}{$somevalue}),$value);

should be
push(@{$somehash{$somevalue}},$value);
0
 

Author Comment

by:grimjb
ID: 8224437
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
Independent Software Vendors: 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 3

Accepted Solution

by:
prady_21 earned 200 total points
ID: 8224827
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
 
LVL 3

Expert Comment

by:prady_21
ID: 8224914
The problem with ur code was that a semi colon was missing at the end of $index = "elephant"
That was the error
0
 
LVL 3

Expert Comment

by:prady_21
ID: 8224930
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
 

Author Comment

by:grimjb
ID: 8226087
That hit the spot :) I can't believe I missed a semi.

thanks for the help!!
0
 

Author Comment

by:grimjb
ID: 8226096
prady_21 was really good to put up with a perl newbie like me :) thanks again
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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

801 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