Well, if you don't want to use a module, you could try something like the following code - it basically breaks the job down into several parts. The only major requirement is that all your quotes should be valid pairs (you should probably add a test to check that you have an even number of quotes and that you have the number of fields per line that you expect).

  1. Pull out the quoted sections
  2. Replace ',' with '_comma_' in the quoted sections
  3. Restore the quoted sections back into position
  4. Safely split on ',' (since quoted commas are now '_comma_')
  5. Replace '_comma_' with ','

Here's the code:

use strict; my @output; while(<DATA>){ chomp; next unless $_ =~ /\S/; # push any quoted stuff (incl. quotes) onto array... (we assume that + all quotes are paired) push my @quoted, ($_ =~ /"([^"]*)"/g); # replace any commas in the array with '_comma_' foreach my $quote(@quoted){ $quote =~ s/,/_comma_/g; } # now replace the ',' versions with the "_comma_" versions $_ =~ s/"[^"]*"/'"' . (shift @quoted) . '"'/ge; # now we can safely split on any commas (quoted commas are now '_com +ma') push @output, [split /,/]; # finally, replace any '_comma_' values with ',' in the latest eleme +nt of output foreach(@{$output[$#output]}){ s/_comma_/,/g; } } # what have we got? foreach(@output){ foreach(@{$_}){ print "$_:"; } print "\n"; } __DATA__ 123,456,"hello, world, goodbye, world",789 123,456,"hello, world, goodbye, world",789,"foo, bar","bar, foo" "hello, world","goodbye, world",123,"foo" "hello" 123,456,"goodbye, world",789
map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2 -$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm@tomandlu.co.uk

In reply to Re: Perl is returning... odd results... from regular expressions. Things matching when they shouldn't, and stuff like that. by Melly
in thread Perl is returning... odd results... from regular expressions. Things matching when they shouldn't, and stuff like that. by Groxx

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.