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.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.