Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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.


Comment on Re: map to hash of arrays
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 scrutinizing the Monastery: (6)
As of 2015-08-01 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (285 votes), past polls