Question

I'm reading a .csv file that was created in Excel with the first line being column headings. One column heading contains an embedded newline. I want to ignore that newline but reading it line-by-line like:

while ( <IN> ) { 
    ...
    }

will treat it as a new line which will break my code (which I haven't written yet). My approach was to read the first line into an array of column headings and process the rest of the lines differently.

Is there maybe a regex I can use somewhere in the while that ignores the newline unless it's the last new line?

Or should I be approaching this differently?

Was it helpful?

Solution

Use one of the Perl modules that handle CSV, such as Text::CSV_XS. Its documentation shows you how to handle embedded newlines. In general, you don't want to spend your time writing another CSV parser; get on with the more important parts of your task!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top