That bit about having run-on lines with ^M delimiters would make me want to try something like this (not tested):
#!/usr/bin/perl use strict; die "Usage: $0 filename.csv\n" unless ( @ARGV and -f $ARGV[0] ); for my $csvname ( @ARGV ) { my $records = read_csv( $csvname ); if ( ref( $records ) ne 'ARRAY' ) { warn "Unable to pull records from file $csvname\n"; next; } elsif ( @$records == 0 ) { warn "No csv data found in file $csvname\n"; next; } do_something( $records ); } sub read_csv { my $filename = shift; open( IN, "<", $filename ) or do { warn "open failed on $filename: $!\n"; return; }; local $/; my $alldata = <IN>; my @records = grep !/^#|^\s*$/, split( /[\r\n]+/, $alldata ); return \@records; } sub do_something { # because just being able to read is seldom enough... }
I suppose if your files are really huge (hundreds of MB), the slurping and splitting might be impractical. But these days, anything up to a 100 MB or so should fit comfortably.

(Updated to fix grammar in the opening sentence. I'd also suggest that "read_csv" should really be called something else, like "read_file_data" -- there's nothing particularly "csv-ish" about that sub.)


In reply to Re: Parsing a text file by graff
in thread 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.