On Sat, May 30, 2009 at 23:32, Raabe, Wesley <wra...@kent.edu> 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. snip
That would be because the second part of a s/// is not a regex, it is a double quote string. What you want is the /e option which interprets the second part as Perl code instead: my $i = 0; while (<IN>) { s/[ ]{3}/"<pgf " . $i++ . ">"/ge; print; } -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/