Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Why does this "bad" grep work so well?

by BrowserUk (Patriarch)
on Apr 16, 2016 at 06:26 UTC ( [id://1160608]=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^2: Why does this "bad" grep work so well?
by Athanasius (Archbishop) on Apr 16, 2016 at 06:41 UTC

    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.
        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:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1160608]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2024-03-29 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found