FAQ
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.

Thanks,

Wesley Raabe
wraabe@kent.edu
Assistant Professor
Textual Editing and American Literature
Kent State University

Search Discussions

  • Chas. Owens at May 31, 2009 at 4:46 am

    On Sat, May 30, 2009 at 23:32, 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.
    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.
  • John W. Krahn at May 31, 2009 at 6:51 am

    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
  • Dr.Ruud at May 31, 2009 at 11:17 am

    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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedMay 31, '09 at 3:32a
activeMay 31, '09 at 11:17a
posts4
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase