Okay. I think I see what you're trying to do now. Based on what I see in the Cookbook on p 140, it looks like you want a hash whose values are array refs. This will effectively get you the "more than one value per hash key" that you're looking for. It is kind of hard to tell what to help you with regarding that particular example without a little smaller target for us to aim for.
The example is fairly simple, except for the fact that instead of assigning values to an array, then setting the array reference to a hash key, it pushes values directly into the hash.
BTW: here is the code I'm referring to:
%ttys = ();
open(WHO,"who|") or die "can't open who: $!";
while (<WHO>) {
($user, $tty) = split;
push( @{$ttys{$user}}, $tty);
}
foreach $user (sort keys %ttys) {
print "$user: @{$ttys{$user}}\n";
}
The one line that looks like it might cause understanding problems is
push( @{$ttys{$user}}, $tty); Basically, push expects a list for its forst argument, but
$ttys{$user} will return a list refernce, not a list. Using
@{...} forces push to evaluate what's in those braces as a list, which it knows how to work with.
GuildensternNegaterd character class uber alles!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.