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?? |
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:
As for why it works. Assuming properly formed input, spliting on '"' means the input to grep is:
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:
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 Section
Seekers of Perl Wisdom
|
|