Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: map to hash of arrays

by dpuu (Chaplain)
on Jul 22, 2004 at 21:13 UTC ( [id://376719]=note: print w/replies, xml ) Need Help??


in reply to map to hash of arrays

It is obviously possible, but it would not be "nifty". In fact, I think it'd be worse solution than just using a C<for> loop in the first place. You just have to build a temportary hash off to the side. But if you're going to build a tmp hash, then you might as well just build the real one.

--Dave
Opinions my own; statements of fact may be in error.

Replies are listed 'Best First'.
Re^2: map to hash of arrays
by emilford (Friar) on Jul 26, 2004 at 14:18 UTC
    That's my issue. I want to build the real hash of arrays without the need of a temporary one. I'm just not sure how to go about doing that.
      Use a while loop, not a map:
      my %hash; my $key; while (<>) { /\S+ : (\S+)/ or die "unexpected line format: $_"; my $value = $1; if (defined $key) { push @{ $hash{$key} }, $value; undef $key; else { $key = $value; } }
      --Dave
      Opinions my own; statements of fact may be in error.
        Why would I want to use this over ccn's solution? Is it a matter of efficiency?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://376719]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-29 10:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found