use Text::xSV;
my $parser = Text::xSV->new( filename=>$datafile, sep=>"\t" );
$parser->read_header;
my %data;
while (my $row_ref = $parser->get_row) {
$data{ $row_ref->[1] } = $parser->extract_hash;
}
The significant changes from your version are:
- xSV doesn't default to a tab. You have to tell it that you want that.
- You don't need to call open_file - it will do that for you if you pass the filename into the constructor.
- The return of read_header cannot be relied on, so don't rely on it.
- I'm using the return of get_row directly. Yes, you can mix positional and name-based logic in Text::xSV.
- Your slicing logic was buggy. But luckily there is no need to do something that complex.
UPDATE: A closer reading of the original question shows that the format differs from the obvious. The first row has an extra # in it that is not lined up with the other fields and needs to be discarded. Here is a solution to that variant:
use Text::xSV;
my $parser = Text::xSV->new( filename=>$datafile, sep=>"\t" );
my @headers = $parser->get_row;
shift @headers; # Get rid of the unwanted #
$parser->bind_fields(@headers);
my %data;
while (my $row_ref = $parser->get_row) {
$data{ $row_ref->[1] } = $parser->extract_hash;
}
|