add 1 to the captured backreference ??

add 1 to the captured backreference ??

am 12.10.2007 11:50:30 von jbl

It seems like it shoukd be simple but it it driving me more crazy than
I already was.

All I want to do is add 1 to each and any number in the string.
ie... 1 becomes 2
All I am doingwith this is putting a literal '+1'

# changeNumbers.pl
# desired output
# string text 2 string text
# string text 2 string text 3 string text
# string text 2 string text 3 4 string text 5 6 string text

use warnings;
use strict;
while ()
{
my $string = $_;
$string =~ s/(\d)/$1+1/g;
print $string;
}
__DATA__
string text 1 string text
string text 1 string text 2 string text
string text 1 string text 2 3 string text 4 5 string text



this is my actual output;

C:\>changeNumbers.pl
string text 1+1 string text
string text 1+1 string text 2+1 string text
string text 1+1 string text 2+1 3+1 string text 4+1 5+1 string text

thanks,

jbl

Re: add 1 to the captured backreference ??

am 12.10.2007 12:12:46 von Peter Makholm

jbl writes:

> use warnings;
> use strict;
> while ()
> {
> my $string = $_;
> $string =~ s/(\d)/$1+1/g;
> print $string;
> }

You have to use the /e flag to you substitution the get the right side
of the substitution evaluated as a perl expression.

//Makholm

Re: add 1 to the captured backreference ??

am 12.10.2007 12:12:59 von kenslaterpa

On Oct 12, 5:50 am, jbl wrote:
> It seems like it shoukd be simple but it it driving me more crazy than
> I already was.
>
> All I want to do is add 1 to each and any number in the string.
> ie... 1 becomes 2
> All I am doingwith this is putting a literal '+1'
>
> # changeNumbers.pl
> # desired output
> # string text 2 string text
> # string text 2 string text 3 string text
> # string text 2 string text 3 4 string text 5 6 string text
>
> use warnings;
> use strict;
> while ()
> {
> my $string = $_;
> $string =~ s/(\d)/$1+1/g;
> print $string;}
>
> __DATA__
> string text 1 string text
> string text 1 string text 2 string text
> string text 1 string text 2 3 string text 4 5 string text
>
> this is my actual output;
>
> C:\>changeNumbers.pl
> string text 1+1 string text
> string text 1+1 string text 2+1 string text
> string text 1+1 string text 2+1 3+1 string text 4+1 5+1 string text
>
> thanks,
>
> jbl

Close, change the following line
$string =~ s/(\d)/$1+1/g;
to
$string =~ s/(\d)/($1+1)/eg;

HTH, Ken

Re: add 1 to the captured backreference ??

am 12.10.2007 12:29:28 von Martien Verbruggen

On Fri, 12 Oct 2007 04:50:30 -0500,
jbl wrote:
> It seems like it shoukd be simple but it it driving me more crazy than
> I already was.
>
> All I want to do is add 1 to each and any number in the string.
> ie... 1 becomes 2
> All I am doingwith this is putting a literal '+1'

> $string =~ s/(\d)/$1+1/g;

$string =~ s/(\d)/$1+1/ge;

I would probably do

$string =~ s/(\d+)/$1+1/ge;

depending on whether you want an embedded 14 to increment to 25 or 15,
and 19 to 210 or 20.

The perlop documentation explains what the e modifier does for s///.

Martien
--
|
Martien Verbruggen | +++ Out of Cheese Error +++ Reinstall
| Universe and Reboot +++
|

Re: add 1 to the captured backreference ??

am 12.10.2007 12:41:38 von jbl

On Fri, 12 Oct 2007 10:12:46 +0000, Peter Makholm
wrote:

>jbl writes:
>
>> use warnings;
>> use strict;
>> while ()
>> {
>> my $string = $_;
>> $string =~ s/(\d)/$1+1/g;
>> print $string;
>> }
>
>You have to use the /e flag to you substitution the get the right side
>of the substitution evaluated as a perl expression.
>
>//Makholm

Thanks, now it works. Sometimes I cannot see the forest with all of
those trees in the way.
jbl