Solved

referencing multiple values for a single key

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

929 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

11 Experts available now in Live!

Get 1:1 Help Now