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;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.