Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Using the result of s///

by Anonymous Monk
on Apr 30, 2003 at 20:02 UTC ( [id://254445]=perlquestion: print w/replies, xml ) Need Help??

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

Why does this print the number of matches:
$string = 'This_has_underscores';
print $string =~ s/_/ /g;

While this returns my new string: (Desired result)
$string = 'This_has_underscores';
$string =~ s/_/ /g;
print $string;

Replies are listed 'Best First'.
Re: Using the result of s///
by hardburn (Abbot) on Apr 30, 2003 at 20:11 UTC
    print $string =~ s/_/ /g;

    That line actually parses to something like:

    $matches = $string =~ s/_/ /g; print $matches;

    That's because print is receiving the result returned by the substitution, not the string that is being substituted. And the result returned in a substitution is the number of successful substtutions.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Using the result of s///
by elusion (Curate) on Apr 30, 2003 at 20:15 UTC
    The first example you give prints the result of the substitution (the result of $string =~ s/_/ /g). If you look at the documentation for s///, you will see that it returns the number of substitutions made. It's easier to see in code.
    $string = 'This_has_underscores'; $result = $string =~ s/_/ /g; print $result;
    That's what the first result is equal to. The second example prints the new $string, which is what you want in this case.

    elusion : http://matt.diephouse.com

Re: Using the result of s///
by Jenda (Abbot) on Apr 30, 2003 at 20:19 UTC

    Because that is how it was designed :-) And rightly so (I say this even though I've just been bitten by this in @array = map {s/regexp/replace/} split(/,/, $text)) If the s/// returned the resulting string how would you know whether the regexp matched and whether anything was replaced?

    if ($text =~ s/foo/bar/) { ... } else { ... }

    Jenda
    Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.
       -- Rick Osborne

    Edit by castaway: Closed small tag in signature

Re: Using the result of s///
by Cody Pendant (Prior) on Apr 30, 2003 at 21:01 UTC
    Perhaps it would help to think of it like this -- print is print (an expression). Like "print (2+2)" will print 4. So your first example isn't a value or a string, it's an expression that gets evaluated.
    print ($string =~ s/_/ /g);

    --
    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: Using the result of s///
by artist (Parson) on Apr 30, 2003 at 20:16 UTC
    In your code
    print $string =~ s/_//g
    is acutally
    my $matches = $string =~ s/_//g; print $matches;
    Substitution provides a return value as number of matches. Thus when proceeded by print statement it provides the 'return value' rather than value of the string. When not proceeded by anything, the return value is simply not captured.

    artist
Re: Using the result of s///
by Zaxo (Archbishop) on Apr 30, 2003 at 20:21 UTC

    In the first case, you print the return value from the substitution. In the second, you print the string the substitution was performed on.

    The return from s// is a fairly complex matter which depends on context, the /g option, and whether backreference capturing is attempted. See perlop and perlre for details.

    After Compline,
    Zaxo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-04-18 21:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found