Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Pattern Matching issue

by rookie_monk (Novice)
on Sep 13, 2010 at 19:53 UTC ( #860021=perlquestion: print w/replies, xml ) Need Help??

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

Good Afternoon, I cannot seem to figure out why the 3rd pattern match(if statement) does not get triggered. Below is the code and also the results. Thanks in advance for your help.
use strict; my $SetName = "MR_\\1\\2\\3"; my $orig1 = "P"; my $orig2 = "A"; my $orig3 = "T"; my $new1 = "\\\\3"; my $new2 = "\\\\2"; my $new3 = "\\\\1"; if ($new1 =~ /\\3/) { if ($SetName =~ /\\3/) { print "hi!! 3\n"; $SetName =~ s/$new3/$orig1/; } if ($SetName =~ /\\2/) { print "hi!! 2\n"; $SetName =~ s/$new2/$orig2/; } if ($SetName =~ /\\1/) { print "hi!! 1\n"; $SetName =~ s/$new1/$orig3/; } print "SetName = $SetName\n"; } The output is: Hi!! 3 Hi!! 2 SetName = MR_PA\3
Not sure why it didnt print "Hi!! 1". Thanks again.

Replies are listed 'Best First'.
Re: Pattern Matching issue
by Anonymous Monk on Sep 13, 2010 at 19:59 UTC
    Because
    if ($SetName =~ /\\1/)
    cannot match
    SetName = MR_PA\3
    1 and 3 are different
      That is odd. The 1st match shouldve replaced "\1". Hmmm
        You have typos. Note how you transit your 'new' variables 3,2,1 not 1,2,3, which would yield your desired result. A debugger would have made this clear. Check out Basic debugging checklist.
Re: Pattern Matching issue
by changma_ha (Sexton) on Sep 16, 2010 at 09:27 UTC

    hi rookie Monk. What Anonymous monk said was right. after the first match $SetName will be $SetName= MR_P\\2\\3 and after the second match it will be $SetName= MR_PA\\3 so when the third match comes it cannot match it.... Thats why Hi!!1 does not get executed. hoope this will help.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (1)
As of 2023-09-23 05:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?