airblaine has asked for the wisdom of the Perl Monks concerning the following question:

I have the folowing for renumbering lines in a file, but the first line contains a % and the last line contains a %. How would I have the folowing script ignore this character?
$file = "myfile.txt"; open (FILE,"$file") || die $!; while (<FILE>) { s/^(N\d+ )?/N$. /; } continue { print; }
Thanks for your help!

Replies are listed 'Best First'.
Re: Ignoring a Special Character
by DamnDirtyApe (Curate) on Aug 29, 2002 at 23:10 UTC

    Try this:

    #! /usr/bin/perl -w use strict ; $|++ ; my $line = 0 ; while ( <DATA> ) { next if /^%/ ; s/^(?:N\d+\s?)?(?{$line++})/N$line / ; } continue { print ; } exit ; __DATA__ % This is the first line. N1 Hello there N3 I'm a line of text % but I won't be counted. N5 Here I am, number three! I don't have a line number, but I should be four. % End of text

    Output:

    % This is the first line. N1 Hello there N2 I'm a line of text % but I won't be counted. N3 Here I am, number three! N4 I don't have a line number, but I should be four. % End of text

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche