Hello Monks,

I am after some advice on how to read a csv file using Text::CSV_XS from line X into an array. The csv files i am working with have an unusual header so if i try to read the header an error is returned.

I can read a normal csv file with the code below, starting from line 42 in this case -- the code is essentially from CSV_XS synopsis with a few modifications.

#!/usr/bin/perl -w #use strict; #use warnings; #use diagnostics; use Text::CSV_XS; my $file = 'test.csv'; #csv file name my @rows; # array that will store csv values my $csv = Text::CSV_XS->new ({ binary => 1 }) or die "Cannot use CSV: ".Text::CSV->error_diag (); #open file open my $FH, "<:encoding(utf8)", "$file" or die "$file: $!"; #read file in while loop my $i = 1; while (my $row = $csv->getline ($FH) ) { if ($i > 42) {push @rows, $row;} $i++ } $csv->eof or $csv->error_diag (); #close file close $FH; #print contents of @rows array for my $print_rows (@rows) { print "@$print_rows\n"; }

I tried replacing getline with getline_all but i'm new to perl and not certain of the syntax or if it will even work as i expect. Or is a different loop set-up possible so the initial lines won't be parsed.

Thanks for any advice


In reply to Text::CSV_XS read file from line X by tw

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.