Solved

referencing multiple values for a single key

Posted on 2002-04-02
4
171 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
  • 2
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 100 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
HTTP Error 502.2 - Bad Gateway 3 234
pipe to sed or perl, please help 5 137
combine multiple lines 2 74
Matching a random pattern with one common character 2 86
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

730 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