brx has asked for the wisdom of the Perl Monks concerning the following question:
*update* : bad link corrected
*update2* : reply to myself using B::Deparse => Re: strange usage of s/// -- I'm still confused :)
*update3* : problem solved, it's a Perl 5.14 and 5.16 bug.
Perl v5.18.1 handles them as one would expect:perl -MO=Deparse -e '$_="abcdef"; s/@{[s".."."gr]}\K$"//; print' $_ = 'abcdef'; s[@{[s/.././gr];}\K$"][]; print $_; -e syntax OKperl -MO=Deparse -e '$_="abcdef"; s/@{[s".."."gr]}\K //; print' $_ = 'abcdef'; s[@{[s/.././gr];}\K ][]; print $_; -e syntax OK
With Perl 5.14 and 5.16, there is a bug and some quantum tunnelling in s/// (yep! ;):
perl -MO=Deparse -e '$_="abcdef"; s/@{[s".."."gr]}\K$"//; print' $_ = 'abcdef'; s[@{[s/.././rg];}\K][$"]; print $_; -e syntax OK
perl -MO=Deparse -e '$_="abcdef"; s/@{[s".."."gr]}\K //; print' $_ = 'abcdef'; s[@{[s/.././rg];}][\\K ]; print $_; -e syntax OK
original post:
Dear Monks,
Could you, please, help me to understand one mysterious golf solution posted by "tails" at http://golf.shinh.org/p.rb?Bisection . (problem: read line, eg "abcdef", and print bisection, ie "abc def")
This is not the best answer (shortest) but it seems very strange... Here is the code (http://golf.shinh.org/reveal.rb?Bisection/tails_1377006056&pl => http://golf.shinh.org/reveal.rb?Bisection/tails+%28%3F%29_1377006539&pl):
#!perl -pl s/@{[s".."."gr]}\K$"//
*update* : why $" is different from literal space character in this regex? how this regex can add something to $_?
My other strange tests
$_="abcdef"; s/@{[s".."."gr]}/$& /;print # "...abcdef" $_="abcdef"; s/@{[s".."."gr]}//;print # "...abcdef" $_="abcdef"; s/@{[s".."."gr]}\K //;print # "\K def"
FYI, "tails" also wins the contest with:
#!perl -p s/.(?0).|\K/ $&/
Nice usage of (?0), tails! :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: strange usage of s///
by smls (Friar) on Sep 03, 2013 at 17:21 UTC | |
by davido (Cardinal) on Sep 03, 2013 at 17:30 UTC | |
by brx (Pilgrim) on Sep 03, 2013 at 20:35 UTC | |
by brx (Pilgrim) on Sep 03, 2013 at 20:27 UTC | |
|
Re: strange usage of s///
by brx (Pilgrim) on Sep 03, 2013 at 21:01 UTC | |
by smls (Friar) on Sep 03, 2013 at 22:25 UTC | |
by brx (Pilgrim) on Sep 04, 2013 at 17:33 UTC | |
|
Re: strange usage of s///
by smls (Friar) on Sep 03, 2013 at 16:16 UTC | |
by davido (Cardinal) on Sep 03, 2013 at 17:26 UTC | |
by smls (Friar) on Sep 03, 2013 at 17:36 UTC | |
by brx (Pilgrim) on Sep 03, 2013 at 20:16 UTC | |
by brx (Pilgrim) on Sep 03, 2013 at 20:13 UTC |