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

Hello, I am reading a large file and trying to strip off some trailing characters from the words captured through matching and then print them. It seems only one of the substitution works correctly and others don't. Please look at the code below. The script is correctly removing the "_x_s" from the words but it does not remove other ones..

# Matched element is passed to $pat for further processing. $pat = $1; $pat =~ s/_x_x$//i; #This is not working. $pat =~ s/_x_s$//i; #This is working. $pat =~ s/_x$//i; #This is not working. print $pat;
Kind of puzzling why one works and the others don't. Thanks for reading monks. Sorry if I am doing something egregiously wrong in my code.

Replies are listed 'Best First'.
Re: End of line anchor question
by toolic (Bishop) on Apr 23, 2012 at 18:50 UTC
    It seems to work for me:
    use warnings; use strict; while (<DATA>) { my $pat = $_; $pat =~ s/_x_x$//i; #This is not working. $pat =~ s/_x_s$//i; #This is working. $pat =~ s/_x$//i; #This is not working. print $pat; } __DATA__ foo_x_x boo_x_s moo_x

    Prints:

    foo boo moo

    What does $pat have before s///? See also:

Re: End of line anchor question
by deshdaaz (Novice) on Apr 23, 2012 at 18:53 UTC
    Monks, Please ignore this question. The same anchors were missing in another branch of this matching subroutine..All those which did not work were actually being matched in that part of the program..Thanks!