Current location: Hot Scripts Forums » Programming Languages » Perl » pattern substitution question..


pattern substitution question..

Reply
  #1 (permalink)  
Old 01-07-04, 07:19 AM
gmadhukarreddy gmadhukarreddy is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
pattern substitution question..

How can I write something like

$a =~ s/\d+/eval($b+$c-$d)/g;

where i need to get the evaluated value to be substituted. but it is not evaluating....
Reply With Quote
  #2 (permalink)  
Old 01-07-04, 09:26 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by gmadhukarreddy
How can I write something like

$a =~ s/\d+/eval($b+$c-$d)/g;

where i need to get the evaluated value to be substituted. but it is not evaluating....

I'm not exactly sure of what you are trying to do. For what you have there, I would do something like this:

Code:
my $regex = $b +$c - $d;
$a =~ s/\d+/$regex/g;
Can you not get away with doing this though:

Code:
$a = $b + $c - $d;
??

Maybe I'm missing your point?

These might help:
[regex links]
* http://www.perldoc.com/perl5.8.0/pod...lar-expression
http://www.perldoc.com/perl5.8.0/pod/perlfaq6.html
http://www.perldoc.com/perl5.8.0/pod/perlre.html
http://www.perldoc.com/perl5.8.0/pod/perlrequick.html
http://www.perldoc.com/perl5.8.0/pod/perlretut.html
[/regex links]

* This one in particular.


~Charlie
Reply With Quote
  #3 (permalink)  
Old 01-07-04, 10:51 AM
gmadhukarreddy gmadhukarreddy is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
actually, i have a line like ....

123..345,456..567,678..789

and i have to substitute each number (like 123) by adding something to it.

so if i have to add 20 to each,

the regular expression should substitute 123 by 143 (i.e.123+20 )
345 by 365
456 by 476 and so on

so how can I write the regular expression ????
Reply With Quote
  #4 (permalink)  
Old 01-07-04, 02:21 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
so how can I write the regular expression ????
Check out the link I posted above with a star on it:

Code:
#!/usr/bin/perl
use warnings;
use strict;

our $val = 0;
my $string = "123..345,456..567,678..789";

print "Content-Type: Text/HTML\n\n";
print "<pre>\n";
print "Original string: $string\n";

$string =~ s/(\d+)(?{$val = $1 + 20;})/$val/g;

print "New string: $string\n";
print "</pre>";
~Charlie
Reply With Quote
  #5 (permalink)  
Old 01-08-04, 12:55 AM
gmadhukarreddy gmadhukarreddy is offline
Newbie Coder
 
Join Date: Jan 2004
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
thanks

your code works great.......thanks charlie
Reply With Quote
  #6 (permalink)  
Old 01-08-04, 09:38 AM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by gmadhukarreddy
thanks charlie
No worries.

~Charlie
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ADO Recordsets question vbNovice Visual Basic 0 12-22-03 10:25 PM
Substitution question $_eq Arowana Perl 2 12-10-03 10:01 AM
Perl-PHP software Copyright question! kevin PHP 8 10-29-03 08:03 AM
PHP header question khadlock PHP 2 09-23-03 08:17 AM
Web Stat Question ossi44 New Members & Introductions 2 06-13-03 09:36 AM


All times are GMT -5. The time now is 04:51 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.