Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
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
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 contemplating the Monastery: (13)
As of 2014-10-23 19:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (128 votes), past polls