Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Incredibly stupid substitution question :(

by viffer (Beadle)
on Aug 11, 2010 at 03:07 UTC ( #854238=perlquestion: print w/replies, xml ) Need Help??

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

Folks, I beg your indulgence but I've googled this, searched through your pages, asked colleagues till I'm blue in the face.

This HAS to be the easiest question ever asked and I apologise in advance.

I have a string "Kev.has.a.stupid.perl.question"

All I want to do is replace the full stops with backslash full stop so that I can pass it into an interpolated pax string.

I have tried
$text =~ s/\./\\./g;
but that gives me
'Kev\\.has\\.a\\.stupid\\.perl\\.question'
If I try
$text =~ s/\./\./g;
It gives me the text string I started with

What I want to end up with is
'Kev\.has\.a\.stupid\.perl\.question'.

It can't be that hard. can it?
Cheers
Kev

Replies are listed 'Best First'.
Re: Incredibly stupid substitution question :(
by wwe (Friar) on Aug 11, 2010 at 07:23 UTC
Re: Incredibly stupid substitution question :(
by biohisham (Priest) on Aug 11, 2010 at 03:28 UTC
    One regex-less way to do this is to split around the (.) into an array and join the elements back;
    use strict; use warnings; my $string = "Kev.has.a.stupid.perl.question"; my @array = split /\./, $string; my $fixed = join('\.',@array); print $fixed;
    Though, your regex $text =~ s/\./\\./g; seems to can work fine!.

    update: added links and a bit of details


    Excellence is an Endeavor of Persistence. A Year-Old Monk :D .
      One regex-less way to do this is to split
      Uhm, you do know split, don't you? And you do know the type of its first argument, don't you?

      Indeed, you do. Your "regexp-less" way uses the same regexp as you're trying to avoid. All you did was to move the regexp from s/// to split. You "regexp-less" way does not use less regexpes, or even a simpler one.

      the original regex didn't worked on my system too. I'm using Strawberry Perl 5.10 on WinXP.
Re: Incredibly stupid substitution question :(
by suhailck (Friar) on Aug 11, 2010 at 03:19 UTC
    perl -le '$_=q[Kev.has.a.stupid.perl.question];s/\./\\./g;print' Kev\.has\.a\.stupid\.perl\.question
    This works in my system

    ~suhail
      when I do print, yes it does give me that,
      looking through perl debug it gives me
      Kev\\.has\\.a\\.stupid\\.perl\\.question'

      maybe it's just how it shows up in debug. Cheers

        viffer:

        How are you displaying it under the debugger? I'm seeing the expected value (using perl v5.10.1 under cygwin):

        roboticus@Boink:~$ cat 854238.pl my $text = "Kev.has.a.stupid.perl.question"; $text =~ s/\./\\./g; $text .= "."; print $text; roboticus@Boink:~$ perl 854238.pl Kev\.has\.a\.stupid\.perl\.question. roboticus@Boink:~$ perl -d 854238.pl ...snip... main::(854238.pl:1): my $text = "Kev.has.a.stupid.perl.question"; DB<1> b 3 DB<2> r main::(854238.pl:3): $text .= "."; DB<2> p $text Kev\.has\.a\.stupid\.perl\.question DB<3>

        ...roboticus

Re: Incredibly stupid substitution question :(
by atancasis (Sexton) on Aug 11, 2010 at 05:27 UTC

    I'm going to assume escaping the dots is so that the string can be used inside a regex, in which case, quotemeta will be perfect.

    print quotemeta('Kev.has.a.stupid.perl.question')."\n";

    Produces:

    Kev\.has\.a\.stupid\.perl\.question
Re: Incredibly stupid substitution question :(
by kishore.lakshman (Initiate) on Aug 11, 2010 at 05:12 UTC
    try this $text =~ s/\./\\\./g;
Re: Incredibly stupid substitution question :(
by dasgar (Priest) on Aug 11, 2010 at 09:02 UTC

    Well, if this is driving you crazy, you're not alone. I tried your code on my system (ActiveState Perl 5.12) and used Data::Dumper. When I print the variable (print "$text\n";), it appears just as you want it. When I do print Dumper($text);, it has an extra \ in it just like you're seeing.

    As for why Data::Dumper is not matching the print statement, I'm stumped.

      As for why Data::Dumper is not matching the print statement, I'm stumped.

      Because the output of Data::Dumper can be evaled into the original string, hence the "escaping" of the backslashes.

      This means that the original string only had single backslashes, otherwise Data::Dumper would have shown 4 backslashes in a row!

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

        Cool! Thanks for the explanation. Learned something new.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2022-05-18 12:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (70 votes). Check out past polls.

    Notices?