substitution without affecting the input string

P

Peng Yu

my $string = "The act sat on the mta";
$string =~ s/act/cat/;

The above command will change the content of $string. Is there a way
not to update the input string but to return me a new string.
 
S

sreservoir

my $string = "The act sat on the mta";
$string =~ s/act/cat/;

The above command will change the content of $string. Is there a way
not to update the input string but to return me a new string.

do { (my $new = "The cat sat on the mta") =~ s/mta/mat./; $new }
 
P

Peng Yu

do { (my $new = "The cat sat on the mta") =~ s/mta/mat./; $new }

This is indeed looks awkward to me. I still want to preserve the first
line (my $string = "The act sat on the mta";). To preserve $string, I
have to make a copy of it (as in the following code). Is there a
better way?

my $string = "The act sat on the mta";# I don't want to change this
line
$newstring=$string
$newstring =~ s/act/cat/;#Is there a better way for the following two
lines?
 
S

sreservoir

This is indeed looks awkward to me. I still want to preserve the first
line (my $string = "The act sat on the mta";). To preserve $string, I
have to make a copy of it (as in the following code). Is there a
better way?

my $string = "The act sat on the mta";# I don't want to change this
line
$newstring=$string
$newstring =~ s/act/cat/;#Is there a better way for the following two
lines?

sub replret_e($$$) {
my $str = $_[0];
$str =~ s/$_[1]/$_[2]/e;
}

replret($string, qw/act/, sub { "cat" });

er. that's still awkward and not exactly efficient.
 
J

John Bokma

Peng Yu said:
my $string = "The act sat on the mta";
$string =~ s/act/cat/;

The above command will change the content of $string. Is there a way
not to update the input string but to return me a new string.

perl -e 'my $string = "The act sat on the mta";
( my $new_string = $string ) =~ s/act/cat/;
print "old: $string\nnew: $new_string\n";
'
old: The act sat on the mta
new: The cat sat on the mta
 
S

sln

This is indeed looks awkward to me. I still want to preserve the first
line (my $string = "The act sat on the mta";). To preserve $string, I
have to make a copy of it (as in the following code). Is there a
better way?

my $string = "The act sat on the mta";# I don't want to change this
line
$newstring=$string
$newstring =~ s/act/cat/;#Is there a better way for the following two
lines?

"#Is there a better way for the following two" ?

No, there isin't.
Still have to copy the string.
What do you mean by "better way" ?
Just take out the newline, and you have 1 line:

$newstring=$string; $newstring =~ s/act/cat/;

Or better yet take out all the newlines in your code
and you will have a 1 liner.

-sln
 
R

Randal L. Schwartz

Peng> This is indeed looks awkward to me. I still want to preserve the first
Peng> line (my $string = "The act sat on the mta";). To preserve $string, I
Peng> have to make a copy of it (as in the following code). Is there a
Peng> better way?

s/// is always in place, for efficiency. If you want to preserve the
old string, you have to start with a copy.

It's not any more awkward than a language that *only* does copies and you want
the updated string back in the original variable. You'd probably complain
about that too then. :)

print "Just another Perl hacker,"; # the original
 
J

Jochen Lehmeier

Peng> better way?
It's not any more awkward than a language that *only* does copies and
you want the updated string back in the original variable. You'd
probably complain about that too then. :)

Oh, *I* usually complain when the languages does not have built-in
operators for regular expressions.

Oh wait, that includes almost every language except Perl. *sigh*
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top