Solved

referencing multiple values for a single key

Posted on 2002-04-02
4
173 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 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

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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

724 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