Solved

referencing multiple values for a single key

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
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…
This video discusses moving either the default database or any database to a new volume.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now