|
From: Dr.Ruud on 2 Nov 2005 07:31 I was trying the s///x syntax and got unexpected results. Somebody cares to explain? Simplified example: #!/usr/bin/perl use warnings; use strict; local ($,, $\) = ("\t", "\n"); my $x; $_ = "abc 123 def 123 ghi"; $x = s/ # Replace 1 # ONE 2 # TWO 3 # THREE / # by 4 # FOUR 5 # FIVE 6 # SIX /gsx; # global, single line, extended format print 'Made', $x, 'replacements.'; print; This printed: Made 2 replacements. abc # by 4 # FOUR 5 # FIVE 6 # SIX def # by 4 # FOUR 5 # FIVE 6 # SIX ghi I expected: abc 456 def 456 ghi -- Affijn, Ruud & perl, v5.8.6 built for i386-freebsd-64int "Gewoon is een tijger."
From: Anno Siegel on 2 Nov 2005 08:00 Dr.Ruud <rvtol+news(a)isolution.nl> wrote in comp.lang.perl.misc: > I was trying the s///x syntax and got unexpected results. > Somebody cares to explain? > > Simplified example: > > #!/usr/bin/perl > > use warnings; > use strict; > > local ($,, $\) = ("\t", "\n"); > my $x; > > $_ = "abc 123 def 123 ghi"; > > $x = s/ # Replace > 1 # ONE > 2 # TWO > 3 # THREE > / # by > 4 # FOUR > 5 # FIVE > 6 # SIX > /gsx; # global, single line, extended format > > print 'Made', $x, 'replacements.'; > print; > > > This printed: > > Made 2 replacements. > abc # by > 4 # FOUR > 5 # FIVE > 6 # SIX > def # by > 4 # FOUR > 5 # FIVE > 6 # SIX > ghi > > I expected: abc 456 def 456 ghi > The changes by /x only affect the regex proper. The replacement part is still an ordinary double-quotish string. Anno -- If you want to post a followup via groups.google.com, don't use the broken "Reply" link at the bottom of the article. Click on "show options" at the top of the article, then click on the "Reply" at the bottom of the article headers.
From: Paul Lalli on 2 Nov 2005 08:02 Dr.Ruud wrote: > I was trying the s///x syntax and got unexpected results. > Somebody cares to explain? > > Simplified example: > > #!/usr/bin/perl > > use warnings; > use strict; > > local ($,, $\) = ("\t", "\n"); > my $x; > > $_ = "abc 123 def 123 ghi"; > > $x = s/ # Replace > 1 # ONE > 2 # TWO > 3 # THREE > / # by > 4 # FOUR > 5 # FIVE > 6 # SIX > /gsx; # global, single line, extended format > > print 'Made', $x, 'replacements.'; > print; > > > This printed: > > Made 2 replacements. > abc # by > 4 # FOUR > 5 # FIVE > 6 # SIX > def # by > 4 # FOUR > 5 # FIVE > 6 # SIX > ghi > > I expected: abc 456 def 456 ghi Your expectations were incorrect. The /x modifier causes whitespace in the *pattern match* to be ignored. The replacement portion of a s/// operation is not a pattern match - it is a double-quoted string. /x has no effect on this replacement. Paul Lalli
From: Gunnar Hjalmarsson on 2 Nov 2005 08:09 Dr.Ruud wrote: > I was trying the s///x syntax and got unexpected results. > Somebody cares to explain? > > Simplified example: > > #!/usr/bin/perl > > use warnings; > use strict; > > local ($,, $\) = ("\t", "\n"); > my $x; > > $_ = "abc 123 def 123 ghi"; > > $x = s/ # Replace > 1 # ONE > 2 # TWO > 3 # THREE > / # by > 4 # FOUR > 5 # FIVE > 6 # SIX > /gsx; # global, single line, extended format > > print 'Made', $x, 'replacements.'; > print; > > > This printed: > > Made 2 replacements. > abc # by > 4 # FOUR > 5 # FIVE > 6 # SIX > def # by > 4 # FOUR > 5 # FIVE > 6 # SIX > ghi > > I expected: abc 456 def 456 ghi Try: $x = s/ # Replace 1 # ONE 2 # TWO 3 # THREE /456/gx; # global, extended format Note that the /s modifier is redundant (see "perldoc perlre"). -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
From: Dr.Ruud on 2 Nov 2005 08:37
Gunnar Hjalmarsson schreef: > [s/ 123 #one-two-three / 456 #four-five-six /x] > > Try: > > $x = s/ # Replace > 1 # ONE > 2 # TWO > 3 # THREE > /456/gx; # global, extended format Of course what is in my real and working code is a lot more like that. But I like the commented format much better and was real disappointed that it didn't work. > Note that the /s modifier is redundant (see "perldoc perlre"). I don't consider the /s modifier redundant. It was not needed in my example, so maybe you meant "redundant here"? -- Affijn, Ruud "Gewoon is een tijger." |