Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: map to hash of arrays

by emilford (Friar)
on Jul 26, 2004 at 14:18 UTC ( [id://377423]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^3: map to hash of arrays
by dpuu (Chaplain) on Jul 26, 2004 at 15:22 UTC
    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?
        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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2024-03-28 19:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found