Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Re: Re: another 'array to hash' question

by stajich (Chaplain)
on Dec 10, 2003 at 10:40 UTC ( [id://313746]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Re: Re: another 'array to hash' question
in thread another 'array to hash' question

Does the CSV have a header line which describes the columns? I often do this
chomp($headerline = <INFILE>); my @header = split(/\t/,$headerline);
(although you could just define one relative to the code
my @header = qw(fn ln age);
Now when reading in the file
while( <INFILE>) { chomp; my $c = 0; my @columns = split(/\t/,$_); push @a, { map { $header[$c++] => $_ } @columns }; }

Replies are listed 'Best First'.
Re: Re: Re: Re: another 'array to hash' question
by etcshadow (Priest) on Dec 10, 2003 at 10:55 UTC
    That's what I would have suggested, with one change: use a hash slice.
    # instead of this my $c = 0; my @columns = split(/\t/,$_); push @a, { map { $header[$c++] => $_ } @columns }; # do this my %row; @row{@header} = split(/\t/,$_); push @a, \%row;
    They're functionally equivalent, I know... but it's just that since what you are doing is really a hash-slice, why not use hash-slice syntax?

    ------------
    :Wq
    Not an editor command: Wq

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://313746]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.