Result from a regex substitute

J

Julia deSilva

Hi there all,

I sure this is really obvious but what's the syntax for this please:

my $string1 = "mark had a little dog";
$string1 =~ s/dog/lamb/g; # $string1 is "mary had a little lamb"


But ................
my $string1 = "mark had a little dog";
my $string2 = $string1 =~ s/dog/lamb/g;
This returns
$string2 = 1 (presumably a boolean for substitute done)
whereas I want it to return $string2 = "mary had a little lamb";

Thanks in advance
 
T

Tassilo v. Parseval

Also sprach Julia deSilva:
I sure this is really obvious but what's the syntax for this please:

my $string1 = "mark had a little dog";
$string1 =~ s/dog/lamb/g; # $string1 is "mary had a little lamb"


But ................
my $string1 = "mark had a little dog";
my $string2 = $string1 =~ s/dog/lamb/g;
This returns
$string2 = 1 (presumably a boolean for substitute done)

Yes, it returns the number of replacements done. You need to add
parens:

(my $string2 = $string1) =~ s/dog/lamb/g;

Tassilo
 
C

Chris

Julia said:
Hi there all,

I sure this is really obvious but what's the syntax for this please:

my $string1 = "mark had a little dog";
$string1 =~ s/dog/lamb/g; # $string1 is "mary had a little lamb"


But ................
my $string1 = "mark had a little dog";
my $string2 = $string1 =~ s/dog/lamb/g;
This returns
$string2 = 1 (presumably a boolean for substitute done)
whereas I want it to return $string2 = "mary had a little lamb";

If you want to keep the $string1 to remain as "mark had a little dog"
and have $string2 be the changed string do this:

my string1 = my string2 = "mark had a little dog";
$string2 =~ s/dog/lamb/g;

BTW This substitution won't ever give "mary had a little lamb" it will
give you "mark had a little lamb".
 
J

John Strauss

Hi there all,

I sure this is really obvious but what's the syntax for this please:

my $string1 = "mark had a little dog";
$string1 =~ s/dog/lamb/g; # $string1 is "mary had a little lamb"


But ................
my $string1 = "mark had a little dog";
my $string2 = $string1 =~ s/dog/lamb/g;
This returns
$string2 = 1 (presumably a boolean for substitute done)
whereas I want it to return $string2 = "mary had a little lamb";

Thanks in advance

(my $string2 = $string1) =~ s/dog/lamb/g;

unless you really want to change "mark" to "mary":
my %lut=qw/mark mary dog lamb/;
(my $string2 = $string1) =~ s/(mark|dog)/$lut{$1}/g;



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drop the .thetenant to get me via mail
 
J

Julia deSilva

Thanks everyone,
my $string1 = "mark had a little dog";

Sorry, I did get my nursery rhyme wrong, and for YOUR information, I live 2
miles from Kilmersdon, the village where Jack and Jill fell down the hill,
but that's another story.....
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top