PriNet has asked for the wisdom of the Perl Monks concerning the following question:

Simple simple question for someone who's actually done it...
Why doesn't $String =~ s/chr(12)//g; work?

Has anyone seen this sliced bread thing? I's better than... sliced... well... never mind...

Replies are listed 'Best First'.
Re: Swap Ascii chr() for null string
by AnomalousMonk (Archbishop) on Sep 16, 2017 at 00:38 UTC

    But if you just gotta have executable code in the search pattern, try something like:

    c:\@Work\Perl\monks>perl -wMstrict -MData::Dump -le "my $s = qq{xxx\x0cyy\x0czzz}; dd $s; ;; $s =~ s{ \Q@{[ chr 12 ]}\E }{}xmsg; dd $s; " "xxx\fyy\fzzz" "xxxyyzzz"
    (But on second thought, follow the advice given you here and here by wiser monks than I :)


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

Re: Swap Ascii chr() for null string
by Discipulus (Canon) on Sep 16, 2017 at 10:55 UTC
    Hello PriNet,

    in the TMTOWTDI spirit I repet what I said in ChatterBox yesterday night, but I strongly suggest you to follow the ikegami's way to do it: it is direct, concise and reliable with no need of temp variable. If you have problem with hex values just put some comments above the regex as a cheatsheet: # hex \x0C  is chr(12) and so on.

    What I proposed yesterday was:

    s/${\(chr(12))}//g;

    That is more a trick than a real solution, good to know. See Re: Using 'ord' in a regular expression for substitution where JavaFan originally proposed this solution.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Swap Ascii chr() for null string
by ikegami (Patriarch) on Sep 15, 2017 at 20:57 UTC

    Works for me.

    $ perl -e' my $String = "xxxchr12yyy"; $String =~ s/chr(12)//g; CORE::say $String; ' xxxyyy
      not the string 'chr12', the ascii character (12)...

      Has anyone seen this sliced bread thing? I's better than... sliced... well... never mind...

        But you told it to match the string chr12! You can't place code in a string literal and expect it to be executed.

        You want any of the following:

        s/\014//g; # By octal s/\x0C//g; # By hex s/\x{C}//g; # By hex (leading zeros allowed) s/\N{U+C}//g; # By hex (leading zeros allowed) s/\N{FORM FEED}//g; # By name s/\N{FF}//g; # By alias

        You could also use the character literally.

        my $ff = chr(12); s/\Q$ff\E//g;

        PriNet:

        You can't have arbitrary code in the left of a substitution regular expression. (If you use the e modifier, though, you can do so on the right side.)

        So you either need to use interpolation:

        $form_feed = chr(12); $var =~ s/$form_feed//g;

        Or you can just specify the value using a hex or octal literal \x0c or \014:

        $var =~ s/\014//g;

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.

Re: Swap Ascii chr() for null string
by haukex (Archbishop) on Sep 16, 2017 at 08:09 UTC

    TIMTOWTDI party ;-) These haven't been mentioned yet:

    s/\f//g s/(??{ chr 12 })//g s/ (??{ quotemeta chr 12 }) //gx
Re: Swap Ascii chr() for null string (Perl 6)
by holli (Abbot) on Sep 16, 2017 at 12:26 UTC
    All is better with Perl 6 :-)
    use v6; $_ = chr(65); .say; s/<{chr(65)}>/B/; .say;" # outputs A B
    This made me trip and I had to ask on IRC where MasterDuke++ helped me out. I originally had
    use v6; $_ = chr(65); .say; s/{"here".say; chr(65)}/B/; .say;" # outputs A her +e BA
    which didn't work.

    In Perl 6 code blocks can appear (pretty much) anywhere, and the code even runs here. It's return value just doesn't get used (for a reason I don't know yet) until you also use the angle brackets around the block. I couldn't find that in the docs for some reason.


    holli

    You can lead your users to water, but alas, you cannot drown them.
Re: Swap Ascii chr() for null string
by Anonymous Monk on Sep 15, 2017 at 21:06 UTC
    s/\x0C//g
Re: Swap Ascii chr() for null string
by Anonymous Monk on Sep 17, 2017 at 14:20 UTC
    The first series of replies gave you the correct technical explanation, roboticus gave you your closest and most-direct answer. The rest is noise all-too typical of PerlMonks.