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 15: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