karlberry has asked for the wisdom of the Perl Monks concerning the following question:
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:first junk line then a blank line another junk line fieldname1,fieldname2 value1,value2
Apparently gets the field names ok: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: $!";
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.got columns: fieldname1 fieldname2
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: skip junk lines in csv before header
by Marshall (Canon) on Jul 24, 2022 at 17:36 UTC | |
by karlberry (Sexton) on Jul 24, 2022 at 17:48 UTC | |
|
Re: skip junk lines in csv before header
by tybalt89 (Monsignor) on Jul 24, 2022 at 22:31 UTC | |
by GotToBTru (Prior) on Jul 28, 2022 at 12:19 UTC | |
by AnomalousMonk (Archbishop) on Jul 28, 2022 at 12:53 UTC | |
by LanX (Saint) on Jul 28, 2022 at 13:16 UTC | |
by AnomalousMonk (Archbishop) on Jul 28, 2022 at 15:28 UTC | |
by LanX (Saint) on Jul 28, 2022 at 15:45 UTC |