Marshall has asked for the wisdom of the Perl Monks concerning the following question:

My post at Re^2: Replace commas with spaces between quotes, parsing CSV shows a very weird grep statement from another poster. Normally a grep is a simple filter (yes/no), This statement appears to do a substitution and "work" albeit with run-time warnings. I am wondering why it even works as well as it does?

I re-coded this so that there are no warning and my code works as expected. I did have trouble with making this into a map{} instead of foreach(). Performance wise that doesn't matter. But to understand the syntax issues, any ideas?

Replies are listed 'Best First'.
Re: Why does this "bad" grep work so well?
by BrowserUk (Patriarch) on Apr 16, 2016 at 06:26 UTC

    The easiest fix for the warning is to throw away what I assume is meant to be an anonymous block that is being mistaken for an anonymous hash constructor, and causing the warning.

    Especially as the anonymous block is entirely unnecessary. This produces the same output with no warning:

    $i=0; $line2 = join '', grep { $i++ % 2 ? s/,/ /g : 1 } split /"/,$line;

    As for why it works. Assuming properly formed input, spliting on '"' means the input to grep is:

    1925,47365,2,650187016,1,1, MADE FOR DRAWDOWNS, NEVER P/U ,16,IFC 8112NP,Standalone-6,,,44,10/22/2015,91607,,B24W02651,, PA-3, PURE ,4/28/2015,1,0,,1,MAN,,CUST,,CUSTOM MATCH,0,TRUE,TRUE,O,C48A0D001EF449 +E3AB97F0B98C811B1B,POS.MISTINT.V0000.UP.Q,PROD_SMISA_BK,414D512050524 +F445F504F5331393235906F28561D2F0020,10/22/2015 9:29,10/22/2015 9:30

    Which means the second and fourth parts (for your example and more generally; every odd indexed piece of the list) are the contents of the double quoted strings; and those parts that need to have the commas replaced by spaces.

    The $i++ % 2 ? XXX : YYY construct ensure that the XXX expression will be run for each odd-index list element;p and YYY for the evens.

    The substitution is run on those odds parts; and returns true so the modifed bit is passed through; and the 1 ensure the even bits are passed through unmodifed.

    Job done, reassemble the string with its modifications.

    Finally, this map version is possibly simpler:

    $i=0; $line2 = join '', map{ $i++ % 2 && s/,/ /g; $_ } split /"/,$line;

    and has a better golf score (until you remove the unnecessary anon hash constructor from the original:).


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

      More importantly, the map version will be correct in the case where the substitution fails (because the element contains no comma characters). In the grep version, a failed substitution will cause the grep condition to evaluate to false, and the element will be filtered out.

      FWIW, here’s my variation on the map body, adding the /r modifier to the substitution:

      $i = 0; $line2 = join '', map { $i++ % 2 ? s/,/ /gr : $_ } split /"/, $line;

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        in the case where the substitution fails (because the element contains no comma characters).

        Yes. That's probably the reason for the hash constructor. If you replace it with an anonymous array constructor, it still works and the warning goes away.

        my $i=0; my $line2 = join '', grep{ $i++ % 2 ? [ s/,/ /g ] : 1 } split /"/,$li +ne;

        Ie. An array containing 0 or 1 is still true.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
        In the absence of evidence, opinion is indistinguishable from prejudice.