Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

simple hash question

Posted on 2003-03-27
8
Medium Priority
?
141 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
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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

577 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