Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > search and replace question - help!

Reply
Thread Tools

search and replace question - help!

 
 
jac
Guest
Posts: n/a
 
      05-23-2006
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

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      05-23-2006
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
 
Reply With Quote
 
 
 
 
jac
Guest
Posts: n/a
 
      05-23-2006
thanks plenty Mirco Wahab!
you guys will be hearinglot from me..
im trying to understand perl.. im a perl virgin..

thanks!
jac

 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      05-23-2006
jac wrote:
> 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
 
Reply With Quote
 
jac
Guest
Posts: n/a
 
      05-23-2006

Bernard El-Hagin wrote:
> "jac" <(E-Mail Removed)> wrote:
>
> > 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";

>
>
> 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

 
Reply With Quote
 
jac
Guest
Posts: n/a
 
      05-23-2006
oops, sorry.. did that came out bad... i was just trying to post a
smile.
from now on, only perl post.

Thanks,
jac

 
Reply With Quote
 
jac
Guest
Posts: n/a
 
      05-23-2006

David Squire wrote:
> jac wrote:
> > 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.


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

jac,

 
Reply With Quote
 
David Squire
Guest
Posts: n/a
 
      05-23-2006
jac wrote:
> David Squire wrote:
>> jac wrote:
>>> 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.

>
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Search and replace question laredotornado Perl Misc 2 07-30-2011 11:23 AM
simple question reag search and replace sivga Perl Misc 2 11-07-2007 11:52 PM
search within a search within a search - looking for better way...my script times out Abby Lee ASP General 5 08-02-2004 04:01 PM
Search and replace with NIO and Regex? Mark McKay Java 3 01-21-2004 05:29 PM



Advertisments