[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

referencing multiple values for a single key

Posted on 2002-04-02
4
Medium Priority
?
176 Views
Last Modified: 2010-03-05
OK, I need to create a hash where there can be anywhere from 1 to 5 values for any given key. I am doing this with the recipe from O'Reilly's Perl Cookbok (p. 140 Hashes w/ Multiple Values per Key), which makes $hash{$key} a reference to an array containing the values for the key.

I insert values into the has like this:

push( @{$hash{$key}}, $m );

So, let's say I loop through this and end up with the following key/value pairs:
key_1 value_1
key_2 value_1
key_2 value_2

So far, so good. I then need to write this out to a data file which will be used as parameters for another script. After opening a file handle, I write to the file like this:

foreach $key (sort keys %hash) {
     print(DATA_FH "$key @{$hash{$key}}\n");
}

Now, what I get in the data file is this:
key_1 value_1
key_2 value_1 value_2

but what I need is this:
key_1 value_1
key_2 value_1
key_2 value_2

In other words, if the key is a reference to 1 value, just write the line and move on through the foreach loop. If the key is a reference to more than 1 value I need to print the key/value for the first value and a new line, the key/value for the second value, etc., for up to 5 values.

How best can I accomplish this?
Thanks
0
Comment
Question by:frenomulax
[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
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 400 total points
ID: 6914341
foreach $key (sort keys %hash) {
    foreach $value ( @{$hash{$key}} ){
        print DATA_FH "$key $value\n";
    }
}
0
 

Author Comment

by:frenomulax
ID: 6914703
Thanks, seems obvious now that I see it. I was thinking I needed to somehow reference the individual key/values. As an aside, how would I address a single individual pair on its own, such as key_2/value_2 ? I can think of at least one situation where that would be helpful for building this tool.
0
 
LVL 84

Expert Comment

by:ozo
ID: 6915202
Are you asking how to access $hash{key2}[1] ?
0
 

Author Comment

by:frenomulax
ID: 6915826
From: ozo
Are you asking how to access $hash{key2}[1] ?

Yep, that was exactly what I need. Thanks again for your help.
0

Featured Post

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!

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…
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 …
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

650 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