Amit Koren wrote:
Hi all.
Hello,
Is there a way to insert an arithmetic calculation into a substitute
operation, keeping all "non-integers" AS THEY WERE ?
Example:
$string="blah 10 20";
I need to add 5 to the 10, and add 7 to the 20.
So after the substitution, $string will contain: "blah 15 27".
** Note - i MUST preserve the amount of <spaces> inside the string.
I tried the following:
$string =~ s/(\w+)(\s+)(\d+)(\s+)(\d+)/$1$2($3+5)$4($5+7)/;
You need to use the /e option so that the string half of the
substitution operator is evaluated as perl code:
$ perl -le'
my $string = "blah 10 20";
print $string;
$string =~ s/(\w+)(\s+)(\d+)(\s+)(\d+)/"$1$2" . ( $3 + 5 ) . $4 . ( $5 +
7 )/e;
print $string;
'
blah 10 20
blah 15 27
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/