search and replace question - help!

J

jac

hi everyone...

i have this code..:
--------------------------------
my $pointer="encode";
my $dir="/home/elizar/files";

open (ELI, "$dir/silabs") || die "Can't open file. Reason $! \n";
open (ZAR, ">$dir/silabs.new") || "Can't write file. Reason: $! \n";
while (<ELI>) {
# print "$_";
if ($_ =~ m/$pointer/)
{
$_ =~ s/$ww/$ww+1/g;
}
print ZAR $_ ;
}
close (ZAR);
close (ELI);
system "cp $dir/silabs $backup/silabs_$date";
system "mv $dir/silabs.new $dir/silabs";
----------------------------------


what i want it to replace $ww with $ww+1, that is, if $ww=20,
replaced it with 21.. its not working.. its been bugging me..

but when:
$_ =~ s/20/21/g;

works fine.

What am i missing?

THanks!

jac
 
M

Mirco Wahab

Hi jac
what i want it to replace $ww with $ww+1, that is, if $ww=20,
replaced it with 21.. its not working.. its been bugging me..

but when:
$_ =~ s/20/21/g;

works fine.

What am i missing?

Maybe it works if you use
the [e]val mode of the substitute context:

instead of
$_ =~ s/20/21/g;

you could use:
s/ (\d) / $1 + 1 /egx;

BTW: m/../ and s/../ work on $_ by default.

Example:

...
$text = " this is a bag with 20 apples\n";
print $text;
$_ = $text;

s/ (\d+) / $1 + 1 /exg;
print;
...

prints:
this is a bag with 20 apples
this is a bag with 21 apples

Regards

Mirco
 
J

jac

thanks plenty Mirco Wahab!
you guys will be hearinglot from me..
im trying to understand perl.. im a perl virgin.. ;-)

thanks!
jac
 
D

David Squire

jac said:
thanks plenty Mirco Wahab!
you guys will be hearinglot from me..

In that case you should read *and observe* the posting guidelines of
this group (scan the last week's posts). If you don't observe them (e.g.
quoting context when you respond to post), you will soon find yourself
shouting in the wilderness.

DS
 
J

jac

Bernard said:
That's not your real code, Shirley.

right.. of course (and you have to say that huh.. :)
when i said 'i have this code' im saying that im using it and
not wrote it.. :)

Anyway, thanks! Like i said, im a perl virgin and really appreciate
any help..

regards,
jac
 
J

jac

oops, sorry.. did that came out bad... i was just trying to post a
smile.
from now on, only perl post.

Thanks,
jac
 
J

jac

David said:
In that case you should read *and observe* the posting guidelines of
this group (scan the last week's posts). If you don't observe them (e.g.
quoting context when you respond to post), you will soon find yourself
shouting in the wilderness.

oopss, sorry again.. you we're referring to the quoting of text when
replying..
ok got it. Thanks

jac,
 
D

David Squire

jac said:
oopss, sorry again.. you we're referring to the quoting of text when
replying..
ok got it. Thanks

.... as well as the rest of the posting guidelines. Read them.

DS
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top