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 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.
I don't understand your g-modifier. Why is it there?
I assume that you only want to make the substitution at the start of a line.
#!/usr/bin/perl -w
use strict;
my $fname_inp = "test.inp";
my $fname_oup = "test.oup";
{
open my $fh_inp, "<", $fname_inp or die "'$fname_inp': ", $!;
open my $fh_oup, ">", $fname_oup or die "'$fname_oup': ", $!;
my $pgf = 1;
while ( <$fh_inp> ) {
s/^[ ]{3}/<pgf $pgf>/ and $pgf++;
print $fh_oup $_;
}
close $fh_oup or die "'$fname_oup': ", $!;
}
__END__
--
Ruud
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/