in reply to Text::CSV_XS read file from line X

If you know how many header lines there are you want to skip then you can simply (using 2 lines in the example):

<$FH> for 1 .. 2;

before the while loop. If you need to test each line until you find the first data line you could (where headers lines start with skip):

while (! eof $FH) { my $start = tell $FH; my $line = <$FH>; next if $line =~ /^skip/; seek $FH, $start, 0; last; }
True laziness is hard work

Replies are listed 'Best First'.
Re^2: Text::CSV_XS read file from line X
by tw (Acolyte) on Jan 04, 2011 at 07:57 UTC

    Thanks GrandFather,

    the first solution is the best and also very elegant in this case.

    #!/usr/bin/perl use strict; use warnings; use diagnostics; use Text::CSV_XS; my $file = 'OR10055708.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: $!"; #skip first n (10 in this case) lines <$FH> for 1 .. 10; #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"; }