regular expressions - math on backreferences

C

Chris Nolte

I want to write a Perl script to modify a bunch of my programming run
scripts, where each script will have a number of lines that need to be
incremented.

Here is what I have tried:
#!/usr/bin/perl -w
use strict;

foreach my $in (glob "run.cctm*") {
chomp($in);
open IN, "<$in" or die "can't open $in for input\n";
my $out = "$in.new";
open OUT, ">$out" or die "can't open $out for output\n";
while (<IN>) {
s,NEXTDAY\s*=\s*jul(\d+),NEXTDAY = jul$1+1,;
print OUT;
}
close IN;
close OUT;
}

original: set NEXTDAY = jul22
desired : set NEXTDAY = jul23
result : set NEXTDAY = jul22+1

Thanks for any help on how to do this.

Chris
 
J

Joe Smith

Chris said:
I want to write a Perl script to modify a bunch of my programming run
scripts, where each script will have a number of lines that need to be
incremented.
s,NEXTDAY\s*=\s*jul(\d+),NEXTDAY = jul$1+1,;

s,(NEXTDAY\s*=\s*jul)(\d+),"$1".($2+1),e;
 
C

Chris Nolte

Joe Smith said:
s,(NEXTDAY\s*=\s*jul)(\d+),"$1".($2+1),e;

Thanks, works great.

Can you tell me what the e modifier does? (I can tell that it doesn't
work without it, but besides that...)
 
J

Jürgen Exner

Chris said:
Can you tell me what the e modifier does? (I can tell that it doesn't
work without it, but besides that...)

Why don't you ask he Fine Manual?
- perldoc perlretut
- perldoc perlre

jue
 
C

Chris Nolte

Jürgen Exner said:
Why don't you ask he Fine Manual?
- perldoc perlretut
- perldoc perlre

jue

Because the Fine Manual is not installed on my system. At least, not
for the modules you listed. And I don't see it documented in the
O'Reilly "Learning Perl" book.
 
J

Jürgen Exner

Chris said:
Because the Fine Manual is not installed on my system.

That's a problem. How can anyone expect you to be efficient when the most
important tool is missing.
You should kick your system administrator until he fixes this broken Perl
installation.
At least, not for the modules you listed.

Those are not modules! REs are part of the core Perl language.
And I don't see it documented in the
O'Reilly "Learning Perl" book.

Don't know about that. I don't have "Learning Perl".

jue
 
J

Joe Smith

Chris said:
Because the Fine Manual is not installed on my system. At least, not
for the modules you listed.

The docs are included when perl is properly installed.
You can install perl on Windows PCs and Linux PCs, or
type 'perlre' in http://www.perldoc.com/ .
And I don't see it documented in the O'Reilly "Learning Perl" book.

It should be discussed in the same section that talks about the
'i', 'g', 'm', and 's' modifiers.

It helps to understand the difference between
$_ = "sprintf('%02d:%02d',$1,$2)";
and
$_ = sprintf('%02d:%02d',$1,$2);
to appreciate the 'e' modifier.

-Joe
 
M

Mothra

Chris said:
Because the Fine Manual is not installed on my system. At least, not
for the modules you listed. And I don't see it documented in the
O'Reilly "Learning Perl" book.
FYI The /e modifier evaluates the right side of the subsitution as an
expression.

<rant>
I don't rate that O'Reilly book at all to be honest. It seems to miss
out some of the most fundamental aspects of writing good Perl. How to
use the 'strict' pragma only gets a mention near the end of the book for
example, by which time you might have written dozens of scripts without
it and wondered why your variables don't contain what you thought they
should.

Neither did I think much of perldoc when I was still a novice. Nowhere
is there a simple list of all the modifiers and what they mean. I had
to look through the perlre page, which referred then me to perlop,
through which I had to /\/e to get anywhere near the information I
needed. Not exactly user friendly.
</rant>
 
M

Mothra

That's a problem. How can anyone expect you to be efficient when the most
important tool is missing.
You should kick your system administrator until he fixes this broken Perl
installation.

It might be that you're on a Solaris 8/9 system, in which case the perl
online documentation is installed as ordinary manual pages.

If that is the case, simply add /usr/perl5/man to your MANPATH
environment variable and thereafter run "man" in place of "perldoc",
e.g. man perlop
 
J

Jeff Schwab

Mothra said:
FYI The /e modifier evaluates the right side of the subsitution as an
expression.

<rant>
I don't rate that O'Reilly book at all to be honest. It seems to miss
out some of the most fundamental aspects of writing good Perl. How to
use the 'strict' pragma only gets a mention near the end of the book for
example, by which time you might have written dozens of scripts without
it and wondered why your variables don't contain what you thought they
should.

Neither did I think much of perldoc when I was still a novice. Nowhere
is there a simple list of all the modifiers and what they mean. I had
to look through the perlre page, which referred then me to perlop,
through which I had to /\/e to get anywhere near the information I
needed. Not exactly user friendly.
</rant>

The Camel is certainly better. The modifiers are listed on page 153.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top