FAQ
I wonder if anyone can help me with a question based more on curosity than a
desperate need to know. Say I have a data file with one key/value pair on
each line, which I want to read into a hash. I've been doing it in one of
two ways:

while (<>) {
($key, $value) = split;
$hash{$key} = $value;
}

or

push @array, split while <>;
%hash = @array;

What I'm wondering is if there is anything like:
push %hash, split while <>; # which doesn't work, or
%hash = split while <>; # but that overwrites all but the last pair.

Thanks

Mark C
--
------------------------------------------------------------------------
Dr Mark Crowe John Innes Centre
Norwich Research Park
Tel/Fax: +44 (1603) 450012 Colney
mailto:Mark.Crowe@bbsrc.ac.uk Norwich
NR4 7UH

Search Discussions

  • Jeff 'japhy' Pinyan at Jun 19, 2001 at 2:08 pm
    On Jun 19, mark crowe (JIC) said:
    I wonder if anyone can help me with a question based more on curosity than a
    desperate need to know. Say I have a data file with one key/value pair on
    each line, which I want to read into a hash. I've been doing it in one of
    two ways:

    while (<>) {
    ($key, $value) = split;
    $hash{$key} = $value;
    }

    or

    push @array, split while <>;
    %hash = @array;
    It's better for memory if you do it one-at-a-time, but if you'd really
    like a one-liner, I'd use:

    %hash = map split, <>;

    --
    Jeff "japhy" Pinyan japhy@pobox.com http://www.pobox.com/~japhy/
    I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
    Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
    Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
    Acacia Fraternity, Rensselaer Chapter. Brother #734
    ** Manning Publications, Co, is publishing my Perl Regex book **

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJun 19, '01 at 1:50p
activeJun 19, '01 at 2:08p
posts2
users2
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase