Reputation: 595
I want to ignore headers in my CSV file when using the Perl library Text::CSV_XS.
Is there a better or more proper way to do so than the following:
use Text::CSV_XS;
open(my $fh, "<", "file.csv") or die "file.csv: $!";
my $csv = Text::CSV_XS->new();
#ignore header
my $row = $csv->getline($fh);
while ($row = $csv->getline($fh)) {
#do stuff with the rows...
}
Upvotes: 3
Views: 2571
Reputation: 385655
The following would be slightly better:
# Skip header
$csv->getline($fh);
while (my $row = $csv->getline($fh)) {
...
}
It avoids improper scoping, questionable variable reuse and a needless assignment.
Alternatively, you could use the following:
$csv->column_names( $csv->getline($fh) );
while (my $row = $csv->getline($fh)) {
...
}
You're not using the extra information you're passing to $csv
, so you're doing more work than necessary, but it's self-documenting.
Upvotes: 4