in reply to Re: Why does this "bad" grep work so well?
in thread Why does this "bad" grep work so well?

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,

Replies are listed 'Best First'.
Re^3: Why does this "bad" grep work so well?
by BrowserUk (Patriarch) on Apr 16, 2016 at 06:49 UTC
    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.
      An array containing 0 or 1 is still true.

      And an array (or hash) reference is always true even if the referent is empty.


      Give a man a fish:  <%-{-{-{-<