Adding to a number in replace

R

robin.corcoran

Hello, I have to add to a number in a s/r. My input looks like

Page 1
Page 2
Page 3

I want

Page 6
Page 7
Page 8

Using /e I'm able to add to a number
$string=~s/([0-9]+)/$1+1/e

My problem comes from the fact that I don't want to globally replace
every number. I just want to replace those with "Page #". However, the
/e option is looking for a method when I add text to the replace
string.

$string=~s/Page ([0-9]+)/Page $1+1/e; yields
"Can't call method page without a package or object.

I tried using \Q and \E in my replace string and that doesn't help. I
even tried using \x.. to output "Page" but that doesn't work either.

Is what I'm trying even possible, and if so can someone nudge me in the
right direction?

Thanks,

Robin
 
I

it_says_BALLS_on_your forehead

Hello, I have to add to a number in a s/r. My input looks like

Page 1
Page 2
Page 3

I want

Page 6
Page 7
Page 8

Using /e I'm able to add to a number
$string=~s/([0-9]+)/$1+1/e

My problem comes from the fact that I don't want to globally replace
every number. I just want to replace those with "Page #". However, the
/e option is looking for a method when I add text to the replace
string.

$string=~s/Page ([0-9]+)/Page $1+1/e; yields
"Can't call method page without a package or object.

I tried using \Q and \E in my replace string and that doesn't help. I
even tried using \x.. to output "Page" but that doesn't work either.

Is what I'm trying even possible, and if so can someone nudge me in the
right direction?

positive lookbehind.

use strict; use warnings;


while ( <DATA> ) {
s/(?<=Page)\ (\d+)/$1+1/e;
print;
}

__DATA__
Page 1
Page 2
 
I

it_says_BALLS_on_your forehead

it_says_BALLS_on_your forehead said:
Hello, I have to add to a number in a s/r. My input looks like

Page 1
Page 2
Page 3

I want

Page 6
Page 7
Page 8

Using /e I'm able to add to a number
$string=~s/([0-9]+)/$1+1/e

My problem comes from the fact that I don't want to globally replace
every number. I just want to replace those with "Page #". However, the
/e option is looking for a method when I add text to the replace
string.

$string=~s/Page ([0-9]+)/Page $1+1/e; yields
"Can't call method page without a package or object.

I tried using \Q and \E in my replace string and that doesn't help. I
even tried using \x.. to output "Page" but that doesn't work either.

Is what I'm trying even possible, and if so can someone nudge me in the
right direction?

positive lookbehind.

use strict; use warnings;


while ( <DATA> ) {
s/(?<=Page)\ (\d+)/$1+1/e;
sorry, the above should be:
s/(?<=Page\ )(\d+)/$1+1/e;
 
J

jl_post

Using /e I'm able to add to a number
$string=~s/([0-9]+)/$1+1/e

My problem comes from the fact that I don't want to globally replace
every number. I just want to replace those with "Page #". However, the
/e option is looking for a method when I add text to the replace
string.

$string=~s/Page ([0-9]+)/Page $1+1/e; yields
"Can't call method page without a package or object.


The first way works because "$1+1" is a real expression; that is,
you'd expect the following line of code to function properly:

$var = $1+1;

But replace "$1+1" with "Page $1+1" and you get an expression that
won't compile:

$var = Page $1+1;

The fix is to turn "Page $1+1" into a real expression, like this:

$var = "Page " . ($1+1);

So rewrite your regular expression like this, and it should work
fine:

# (Untested:)
$string =~ s/Page ([0-9]+)/"Page " . ($1+1)/e;

I hope this helps, Robin.

-- Jean-Luc
 
P

Paul Lalli

Hello, I have to add to a number in a s/r. My input looks like

Page 1
Page 2
Page 3

I want

Page 6
Page 7
Page 8

Using /e I'm able to add to a number
$string=~s/([0-9]+)/$1+1/e

My problem comes from the fact that I don't want to globally replace
every number. I just want to replace those with "Page #". However, the
/e option is looking for a method when I add text to the replace
string.

$string=~s/Page ([0-9]+)/Page $1+1/e; yields
"Can't call method page without a package or object.

When you use /e, the replacement must be a valid Perl expression. If
you were writing a normal perl expression, and you wanted to assign
your replacement string to a variable, you know this wouldn't work,
right:
$repl = Page $1+1;
Instead, you'd have to do:
$repl = "Page " . ($1+1);
And that's exactly what you have to do in the regexp:

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

while (<DATA>){

s/^Page (\d+)$/"Page " . ($1 + 3)/e;
print;
}

__DATA__
Page 1
Other stuff 5
Page 2
Page 3

Output:
Page 4
Other stuff 5
Page 5
Page 6

I tried using \Q and \E in my replace string and that doesn't help.

I have no idea what made you think it would. \Q auto-escapes any
regexp-special characters in a double quoted string. What does that
have to do with your current problem?
I
even tried using \x.. to output "Page" but that doesn't work either.

I assume you mean the /x modifier, and again I have no idea what makes
you think this would do anything you're looking for.

Throwing code at the program and seeing what sticks is a poor method of
programming.

Paul Lalli
 
D

Dr.Ruud

Paul Lalli schreef:
s/^Page (\d+)$/"Page " . ($1 + 3)/e;

More complex looking, but still DRY-er (Don't Repeat Yourself) variants:

s/ ^ # at BOL
(Page[ ]) # capture "Page<space>" as $1
(\d+) # capture 1 or more digits as $2
/$1 . ($2 + 3)/ex; # replace captured digits

s/ (?<=^Page[ ]) # zero-width prefix of "^Page<space>"
(\d+) # capture 1 or more digits as $1
/$1 + 3/ex; # replace captured digits
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top