Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How many substitutions took place in a Perl s///g?

Small example:

perl -e '$s="aaabbcc";$c=()=$s=~/a/g;print"$c\n$s\n"' (m//g) outputs

3
aaabbcc

whereas perl -e '$s="aaabbcc";$c=()=$s=~s/a/x/g;print"$c\n$s\n"' (s///g) outputs

1
xxxbbcc

I'd like to do both things at once without having to match first: substitute and know the number of substitutions. Obviously a s///g does not return the number of substitutions in scalar context--unlike m//g does with matches. Is this possible? If yes, how?

perlre, perlvar and perlop provided no help (or I just couldn't find it).

like image 410
musiKk Avatar asked Jun 23 '10 16:06

musiKk


People also ask

What is S in Perl regex?

The substitution operator, s///, is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is − s/PATTERN/REPLACEMENT/;

What is G in Perl?

The “g” stands for “global”, which tells Perl to replace all matches, and not just the first one. Options are typically indicated including the slash, like “/g”, even though you do not add an extra slash, and even though you could use any non-word character instead of slashes.

What is $1 Perl?

$1 equals the text " brown ".

What does !~ Mean in Perl?

!~ is the negation of the binding operator =~ , like != is the negation of the operator == . The expression $foo !~ /bar/ is equivalent, but more concise, and sometimes more expressive, than the expression !($foo =~ /bar/)


1 Answers

s/// does return the number of substitutions made in scalar context. From perlop (emphasis added):

s/PATTERN/REPLACEMENT/msixpogce
Searches a string for a pattern, and if found, replaces that pattern with the replacement text and returns the number of substitutions made. Otherwise it returns false (specifically, the empty string).

Your problem is that you didn't call s/// in scalar context. You called it in list context and then evaluated the assignment (to an empty list) in scalar context. A list assignment in scalar context returns the number of elements produced by the right-hand side of the expression. Since s/// returns a single value (in both list and scalar context) the number of elements is always one even if the s/// didn't do anything.

perl -E "$s='aaabbcc'; $c=()=$s=~s/x/y/g; say qq'$c-$s'"  # prints "1-aaabbcc"

To call s/// in scalar context, omit the =()= pseudo-operator.

perl -E "$s='aaabbcc'; $c=$s=~s/a/x/g; say qq'$c-$s'"  # prints "3-xxxbbcc"
like image 67
Michael Carman Avatar answered Sep 24 '22 14:09

Michael Carman