in reply to Re^2: Embedded Newlines or Converting One-Liner to Loop
in thread Embedded Newlines or Converting One-Liner to Loop

Another implemenation based on Rolf's suggestion:
#!/usr/bin/perl use strict; use warnings; while (my $line = get_CSVline()) { print "$line\n"; } sub get_CSVline { my $buffer; while (!defined($buffer) or is_odd_quotes($buffer) ) { my $temp =<DATA>; $buffer .= $temp; } chomp $buffer; $buffer =~ s/\n/\\n/g; #### make "\n" "visible" ### return $buffer; } sub is_even_quotes { my $string = shift; return !( ($string=~tr/"//) % 2); } sub is_odd_quotes { my $string = shift; return ( ($string=~tr/"//) % 2); } =PRINTS: 1,2,3.3,"\n",4,5 6,7,8,9 6,"\n",7,8 a,b,c,"something\nmore" 1,2,3 1,"x\n","y","z\n",3 "3.5 "" disks" =cut __DATA__ 1,2,3.3," ",4,5 6,7,8,9 6," ",7,8 a,b,c,"something more" 1,2,3 1,"x ","y","z ",3 "3.5 "" disks"
Update: Added one more test case.
Added the '"3.5 "" disks"' test case.

Replies are listed 'Best First'.
Re^4: Embedded Newlines or Converting One-Liner to Loop
by LanX (Saint) on Dec 15, 2016 at 11:09 UTC
    Just a minor nitpick, your test cases doesn't cover an escaped double quote.

    Adding a line like ,"3.5 "" disks", might help. :)

    edit

    And you wouldn't need to check defined $buffer if you used a do-while loop.

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

      Fair enough. Good comments.

      Added a test case. I personally prefer the while(){} vs the do {}while() because I like to see the ending condition right up front. But I understand that opinions vary.

        > I like to see the ending condition right up front.

        Sure, but do-while makes it explicit that you always want the first loop to happen and in tis case the body isn't to large.

        BTW: I like how you solved it with nested iterators, that's IMHO the cleanest approach! :)

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!