country1 has asked for the wisdom of the Perl Monks concerning the following question:


I have multiple CSV files with 2 Header Records. The
first is a title line from the EXCEL spreadsheet and
the second has the column headings from the EXCEL
spreadsheet. Both of these records begin with the
word 'Processor' in column 1 (or column 0 in perl).


I am using the following code to read in the file(s)

open (my $fh,”<”,$file) or die “Can’t open file $file: $!”; while (my $line = <$fh>) { chomp($line); next if $line =~ /^Processor/;

This only eliminates the 1st line in the CSV file.
How would I get perl to also bypass the 2nd line?

20070730 Janitored by Corion: Added code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Deleting Multiple Header Lines From CSV
by FunkyMonk (Bishop) on Jul 26, 2007 at 15:50 UTC
    <$fh> will read a single line from your file, so just add two of them:
    open (my $fh,”<”,$file) or die “Can’t open file $file: $!”; <$fh>; # discard first <$fh>; # two lines while (my $line = <$fh>) { chomp($line); #blah }
Re: Deleting Multiple Header Lines From CSV
by shmf (Novice) on Jul 27, 2007 at 15:26 UTC
    I think that the previous reply is probably the simplest way of doing what you want, and in my opinion the simple is the best.
    However I leave you another suggestion that shall work even if your problematic lines don't occur exactly in the first and second lines. (For example if sometimes you have the 2 headers and other you have only one, or if some other thing gets written before or between the 2 headers)
    open (my $fh,"<",$file) or die "Can't open file $file: $!"; while (my $line = <$fh>) { if($line !~ m/^Processor.*/){ #process only lines which not the on +es starting with "Processor" #(...) } }