in reply to Conditional continued matching with regexes

I'd keep adding lines until I had an even number of double quotes. Only when I had an entire record would I try to munge it with your regex.

Something like

$_ .= <FILE> while s/"/"/ % 2;
(untested), probably checking for errors too.

Replies are listed 'Best First'.
Re^2: Conditional continued matching with regexes
by bart (Canon) on Feb 06, 2007 at 08:11 UTC
    Wah, this is clever... I'd never have thought of that! Well, I will, in the future! ;-) (n.b. you need to add the /g modifier)

    BTW, there's a syntax for tr that serves to just count occurrences, without changing anything. Just leave the RHS empty, which is like replacing characters by themselves while counting, but which is optimizined to skip the replacing.

    tr/"//

    You still need to cater for the abnormal case where the file contains an odd number of quotes, thus, check for eof.

    Update Judging by the tests I've done, the next snippet will work excellently for reading and processing data from <>, and includes processing for the final record for each file, even if incomplete:

    while(<>) { while(!eof and tr/"// % 2) { $_ .= <>; } # ... do stuff with each record in $_ }
      Hurrah!
        So you don't have embedded " within fields?
        "foo"ba
        r",1231