Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: map to hash of arrays

by dpuu (Chaplain)
on Jul 26, 2004 at 15:22 UTC ( #377453=note: print w/ replies, xml ) Need Help??


in reply to Re^2: map to hash of arrays
in thread map to hash of arrays

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.


Comment on Re^3: map to hash of arrays
Download Code
Re^4: map to hash of arrays
by emilford (Friar) on Jul 27, 2004 at 13:53 UTC
    Why would I want to use this over ccn's solution? Is it a matter of efficiency?
      I don't think there's any good reason. ccn assumes, obviously correctly, that the entire data is available in a single string. I didn't realise that. Also, I'd be very tempted to use the /x modifier, and insert some comments.
        That's the part I'm confused about, however. The data is not available in a single string. If I write the output to file, everything is broken down into seperate lines.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2015-07-05 21:27 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 (68 votes), past polls