[Chicago-talk] Simple csv text parsing question

Andrew Rodland arodland at comcast.net
Thu Apr 10 17:32:47 PDT 2008

On Thursday 10 April 2008 01:51:15 pm Mike Ferrari wrote:
> Hi
> So i played with Text::CSV, Text::XS and Text::xSV last night and got
> thoroughly lost :-). each one having its different confusing points.. each
> one saying that they support embedded newlines, each without an explanation
> that i understand. If anyone can point me to an example i would be in your
> debt. I have been beating my head against the wall for 30 or more hours on
> this problem..and its making me question basic simple little things in
> perl... which isn't good.
> Nothing like a simple problem to humble you :-)
> I started thinking about it this morning, essentially what i need is ..
> when the file is read in i need it to not be broken up by the
> interpretation of the newline characters.
> I read up on the $/ "slurp" variable.. which seems to do what i need it to
> do.. not interpret the newlines when taking the data in
> I am having a tough time coming up with a regex (i seriously need to sit
> down for a week and work through/understand perl regexes) that will match
> all occurrences of characters with an embedded newline surrounded by
> quotes...
> essentially like i have in the following (FIND OPEN QUOTE) could be
> something like /"(.*)  match a quote, followed by any character except
> newline, multiple times ... then a \n  and then ?????  find any character
> except newline, multiple times and then ending with a quote.
> This wont take into account things that are not wrapped in quotes or things
> in quotes with multiple newlines.. which makes me want to lean towards
> using a module and not reinventing the wheel. I am going to sit down and
> reread the perldocs for Text::xSV.

Yeah, regex isn't going to do you a very good job. What you need is this: 
don't read the file yourself, and don't regex. That keeps Text::xSV from 
doing its job decently.

You should just be able to do

my $csv = Text::xSV->new(filename => "whatever.csv");

while (my $row = $csv->get_row) {
	do_stuff_with( @$row );

More information about the Chicago-talk mailing list