-3

I'm trying to replace a word in a string. The word is stored in a variable so naturally I do this:

$sentence = "hi this is me";
$foo=~ m/is (.*)/;

$foo = $1;
$sentence =~ s/$foo/you/;
print $newsentence;

But this doesn't work.

Any idea on how to solve this? Why this happens?

1
  • 1
    Always use use strict; use warnings;. It would have pointed out you were using the wrong variable name.
    – ikegami
    Commented Sep 6, 2013 at 14:25

4 Answers 4

4

You have to replace the same variable, otherwise $newsentence is not set and Perl doesn't know what to replace:

$sentence = "hi this is me";
$foo = "me";
$sentence =~ s/$foo/you/;
print $sentence;

If you want to keep $sentence with its previous value, you can copy $sentence into $newsentence and perform the substitution, that will be saved into $newsentence:

$sentence = "hi this is me";
$foo = "me";
$newsentence = $sentence;
$newsentence =~ s/$foo/you/;
print $newsentence;
2

Perl lets you interpolate a string into a regular expression, as many of the answers have already shown. After that string interpolation, the result has to be a valid regex.

In your original try, you used the match operator, m//, that immediately tries to perform a match. You could have used the regular expression quoting operator in it's place:

$foo = qr/me/;

You can either bind to that directory or interpolate it:

$string =~ $foo;
$string =~ s/$foo/replacement/;

You can read more about qr// in Regexp Quote-Like Operators in perlop.

2

You first need to copy $sentence to $newsentence.

$sentence = "hi this is me";

$foo = "me";

$newsentence = $sentence;
$newsentence =~ s/$foo/you/;

print $newsentence;
1
  • The idiomatic perl for this would likely be $old = "me you"; $chg = "me"; ($new = $old) =~ s/$chg/you/; print $new; -- note the ($new = $old) =~ s///; which does the copy and change as one conceptual grouping.
    – user289086
    Commented Sep 6, 2013 at 14:04
1

Even for small scripts, please 'use strict' and 'use warnings'. Your code snippet uses $foo and $newsentence without initialising them, and 'strict' would have caught this. Remember that '=~' is for matching and substitution, not assignment. Also be aware that regexes in Perl aren't word-bounded by default, so the example expression you've got will set $1 to 'is me', the 'is' having matched the tail of 'this'.

Assuming you're trying to turn the string from 'hi this is me' to 'hi this is you', you'll need something like this:

my $sentence = "hi this is me";
$sentence =~ s/\bme$/\byou$/;
print $sentence, "\n";

In the regex, '\b' is a word boundary, and '$' is end-of-line. Just doing 's/me/you/' will also work in your example, but would probably have unintended effects if you had a string like 'this is merry old me', which would become 'this is yourry old me'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.