Raabe, Wesley wrote:
I am using regular expressions to alter a text file. Where my original
file has three spaces to start a paragraph, I want to replace each
instance of three spaces with a bracketed paragraph number, with a
counter for paragraph numbers, <pgf 1>, <pgf 2>, <pgf 3> etc. The
PERL program that I'm using is modeled on the answer to chapter 9,
question 3 in the Learning Perl book (4th ed.).
The WHILE loop that I've crafted is like this:
while (<IN>) {
chomp;
s/\ \ \ /\<pgf\ (?{my $para_num = 1; $para_num++;){print "$para_num";}})\>/gi;
# Replace three spaces with <pgf XX>
print OUT "$_\n";
}
I'm trying to embed the PERL code based on the PERL tutorial
(http://perldoc.perl.org/perlretut.html#A-bit-of-magic%3a-executing-
Perl-code-in-a-regular-expression>, which is noted as an experimental
feature. But it doesn't work (using MAC OSX). The output in my text
file is "<pgf (?{my = 1; ++;){print "";}})" at start of each
paragraph.
Is there a way to do this with AUTO-INCREMENT variable and a FOR loop
outside the regular expression in which the value is inserted inside
the regular expression? My earlier attempts to do it that way always
resulted in no change in the value, just <pgf 1> on every paragraph
time.
my $para_num;
while ( <IN> ) {
s/ /<pgf @{[++$para_num]}>/g;
print OUT;
}
John
--
Those people who think they know everything are a great
annoyance to those of us who do. -- Isaac Asimov
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/