Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: map to hash of arrays

by dpuu (Chaplain)
on Jul 22, 2004 at 21:13 UTC ( #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
Node Status?
node history
Node Type: note [id://376719]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2016-10-01 20:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (7 votes). Check out past polls.