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.
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.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); }
In reply to Parsing a text file by calmthestorm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |