Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

substituting 1 escaped character for another

by craigt (Acolyte)
on Jul 26, 2018 at 16:48 UTC ( #1219326=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to change a '(' in a string to a '<'. I have tried several things, but with no success. I have not been able to substitute or transform, split, or substr. I've quotemeta'd and escaped in the ways I'm familiar with. Any help would be appreciated. Below was my first try. I would like to substitute, index, and split on the '<' character.

$varx = 'healthy(b'; $varx =~ s/\(/\</;

Replies are listed 'Best First'.
Re: substituting 1 escaped character for another
by pryrt (Abbot) on Jul 26, 2018 at 16:59 UTC

    that works for me. could you show us how it doesn't work for you (SSCCE):

    __LINUX__ sh-3.00$ perl -le 'print $x = "healthy(b"; $x =~ s/\(/\</; print $x' healthy(b healthy<b sh-3.00$ perl -v This is perl, v5.8.5 built for i386-linux-thread-multi ... __WINDOWS__ C:\>perl -le "print $x = 'healthy(b'; $x =~ s/\(/\</; print $x" healthy(b healthy<b C:\>perl -v This is perl 5, version 26, subversion 2 (v5.26.2) built for MSWin32-x +64-multi-thread ...
      Also note that the backslash in the replacement part is useless and could be even misleading as someone might think that \< has a special meaning.

      If there are more parentheses to replace, use the /g modifier:


      Also, in case of single character replacements, you can use transliteration:


      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: substituting 1 escaped character for another
by herveus (Prior) on Jul 26, 2018 at 17:02 UTC

    I executed those two lines, and it worked fine for me.

    Can you tell us more about your environment? Because that code should be fine.


      Thanks for responding you all. My server is running Apache/2.0.64 (Win32) mod_perl/2.0.3 and Perl/v5.8.3. My server is an off the shelf PC running the latest version of Windows 10. The application executes in the WWW environment. I test on the latest versions of IE, Edge, FF, O, and Chrome.

        It looks like your problem is somewhere other than those two lines. Perhaps if you could pare down your actual code into the smallest portion that reproduces your problem we could better help figure out what's happening.

        I'm guessing a bit here, but when you say "The application executes in the WWW environment", I'm guessing that you are trying to modify an HTML page? Show us a segment of the HTML page and your code to decode and encode text -> HTML. Your regex will work fine on regular text, but I suspect that is not what you actually have.

        In recent memory, I did a quickie kludge to handle the ampersand character in one "get it done right now" LWP application, $clubName =~ s/&amp;/&/g; &amp is what HTML needs to display the ASCII & character. I suspect something similar is going on. We need more info...

Re: substituting 1 escaped character for another
by kcott (Archbishop) on Jul 27, 2018 at 11:24 UTC

    G'day craigt,

    "I have not been able to substitute or transform, split, or substr."

    You could use any of those methods. I'm assuming by "transform" you mean transliteration (i.e. y/// aka tr///). I acknowledge that some of these solutions have already been posted.

    $ perl -E 'my $x = "A(B"; $x =~ s/\(/</; say $x' A<B
    $ perl -E 'my $x = "A(B"; $x =~ y/(/</; say $x' A<B
    $ perl -E 'my $x = "A(B"; $x = join "<", split /\(/, $x; say $x' A<B
    $ perl -E 'my $x = "A(B"; substr $x, index($x, "("), 1, "<"; say $x' A<B

    Now use Benchmark to compare the different methods. Please post the results as I (and probably others) would be interested.

    — Ken

Re: substituting 1 escaped character for another
by theravadamonk (Scribe) on Jul 30, 2018 at 05:13 UTC

    Do u hv something like this in your code?

    print "Content-Type: text/html; charset=utf-8\n\n";

    Can u change it for a moment to below line and see...

    print "Content-Type: text/text\n\n";

    Now run via web browser

      Thank everyone for responding. I had a Windows 10 update overnight the night after I made this post and this problem was no longer. I don't know. Thanks again.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1219326]
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2023-10-02 20:51 GMT
Find Nodes?
    Voting Booth?

    No recent polls found