Hail noble monks of perl,

I am in need of another pair of eyes to look over the mess I am dealing with. At one point I had all of this working but for some reason it no longer does...

I am trying to parse a text file that is created / updated by multiple users of different OS/applications. The file is plain text comma separated values and "could" contain comments, blank lines and the nasty old dos ^M characters. What I need to do is to be able to break this file into seperate lines while ignoring comments and blank lines. Simple enough right, I thought so too until this piece of code started lumping the whole thing into one line regardless.

The CSV file contents: # This is a comment followed by a blank line # this comment is followed by a run-on line with ^M's humpty dumpty sat on a rock^Mhumpty dumpty fell^MPoor humpty dumpty is + busted all to hell The script in question: #!/usr/bin/perl use strict; # define the csv datafile name (and path if need be) my $csv = 'testing/testfile.csv'; # Process the CSV file &read_csv($csv); # All is well that exits zero... exit; sub read_csv { my @chunks; open(CONF, $csv); while(<CONF>){ chomp; if (/^#/){ # Must be a comment, skip it next; } elsif (/^\s*$/) { # Only contains whitespace, skip it next; } elsif (/^M/){ # Contains dos/mac control characters my @lines = split /^M/, $_; for ( my $i = 0 ; $i <= $#lines ; $i++ ){ push(@chunks, $lines[$i]); } } else { # assumed to be a normal data line push(@chunks, $_); } print "Found data for ", scalar(@chunks), " lines in $csv\n\n"; } close(CONF); }
After all is said and done, the script should print out one line with a number of actual data lines found... I get 0 because it is finding the first comment and lumping the entire file into that one line. I know I must have done something stupid but for the life of me, I dont see it.

Please save me from insanity...

In reply to Parsing a text file by calmthestorm

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.