Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Adding to a number in replace

Reply
Thread Tools

Adding to a number in replace

 
 
robin.corcoran@gmail.com
Guest
Posts: n/a
 
      05-18-2006
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

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      05-18-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
 
 
 
it_says_BALLS_on_your forehead
Guest
Posts: n/a
 
      05-18-2006

it_says_BALLS_on_your forehead wrote:
> (E-Mail Removed) wrote:
> > 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;


> print;
> }
>
> __DATA__
> Page 1
> Page 2


 
Reply With Quote
 
jl_post@hotmail.com
Guest
Posts: n/a
 
      05-18-2006

(E-Mail Removed) wrote:
>
> 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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      05-18-2006
(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
robin.corcoran@gmail.com
Guest
Posts: n/a
 
      05-18-2006
Thanks very much, that did it.

Robin

 
Reply With Quote
 
robin.corcoran@gmail.com
Guest
Posts: n/a
 
      05-18-2006
Thanks Paul and Jean-Luc for your help.

Robin

 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      05-19-2006
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

--
Affijn, Ruud

"Gewoon is een tijger."


 
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
Re: Adding a positive number and a negative number MRAB Python 2 01-31-2009 02:41 AM
Adding a positive number and a negative number EK Python 0 01-30-2009 04:13 AM
Re: [Pyrex] pyrex functions to replace a method (Re: replace a method Greg Ewing Python 2 06-29-2006 05:25 PM
pyrex functions to replace a method (Re: replace a method in class:how?) Brian Blais Python 1 06-27-2006 12:13 PM
OT: Number Nine, Number Nine, Number Nine Frisbee® MCSE 37 09-26-2005 04:06 PM



Advertisments