I have a csv (actually tsv, but I don't think that matters) with junk lines before the header. I want to parse it with Text::CSV. The file looks like:
first junk line then a blank line another junk line fieldname1,fieldname2 value1,value2
It is trivial to skip ahead to the /fieldname/ line, since I know what the first field name is. What I cannot figure out is how to manage the filehandle so that getline_hr_all will then read the rest of the file. Instead, it seemingly reads nothing. My script:
use strict; use warnings; use Text::CSV; my $csv = Text::CSV->new ({ sep_char => ",", eol => "\n", binary => 1, auto_diag => 2, }) || die ("$0: new CSV failed: " . Text::CSV->error_diag () +); my $filename = "/tmp/junk.csv"; # open file. open (my $fh, "<", $filename) || die "CSV open($filename) failed: $!"; my $header_line; while (<$fh>) { next unless /^fieldname/; # skip to header line $header_line = $_; } # parse that line and set our column names. my $status = $csv->parse ($header_line); if (! $status) { die "failed to parse header line from $filename: $header_line"; } my @columns = $csv->fields (); warn "got columns: @columns\n"; $csv->column_names (\@columns); # make a hash of each line, save in list. while (my $ref = $csv->getline_hr ($fh)) { warn "got ref: $ref\n"; } #my $ret = $csv->getline_hr_all ($fh); #warn "ret from _all: @$ret\n"; close ($fh) || die "CSV close($filename) failed: $!";
Apparently gets the field names ok:
got columns: fieldname1 fieldname2
but there is no more output. Not surprisingly, getline_hr_all (the commented-out lines at the end) merely returns the empty list. I'm guessing my reading from the raw filehandle to skip the initial lines is interfering with Text::CSV's reading. Or is it something else entirely? I've perused the pod for Text::CSV for quite a while, as well as doing general web searches, but could not find the answer. Any help appreciated. Thanks.

In reply to skip junk lines in csv before header by karlberry

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.