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

I'm trying to write a simple perl script that replaces some text. The code I wrote below works exactly as I want it to with one exception - I don't want to output a newline character on the last line of the file. I have no idea how to do this. Can someone help me out? Thanks in advance!
------------------------------------------------ #!/usr/bin/perl my $MaxLineLength = 8060; $search = '<COLUMNS>'; $replace = '<DATA>'; $searchEnd = '</COLUMNS>'; $replaceEnd = '</DATA>'; $fileContents = ''; while (<STDIN>) { if (/(^<DATA>\t).*(<\/DATA>$)/) { if (length() <= $MaxLineLength) { print; } } elsif (/(^<COLUMNS>\t).*(<\/COLUMNS>$)/) { s/$search/$replace/g; s/$searchEnd/$replaceEnd/g; print; } }

Replies are listed 'Best First'.
Re: Newline Problem
by polettix (Vicar) on Jun 30, 2005 at 15:06 UTC
    Quick fix, you can work to beautify it:
    #!/usr/bin/perl my $MaxLineLength = 8060; $search = '<COLUMNS>'; $replace = '<DATA>'; $searchEnd = '</COLUMNS>'; $replaceEnd = '</DATA>'; $fileContents = ''; my $linePrefix = ''; while (<STDIN>) { chomp; # Remove newline if (/(^<DATA>\t).*(<\/DATA>$)/) { if (length() <= $MaxLineLength) { print $linePrefix; # Empty only for first line print; $linePrefix = "\n"; } } elsif (/(^<COLUMNS>\t).*(<\/COLUMNS>$)/) { s/$search/$replace/g; s/$searchEnd/$replaceEnd/g; print $linePrefix; print; $linePrefix = "\n"; } }
    Basically, you remove newlines from what you read in, then output newlines before printing a line, not after. The only exception is the first line, of course.

    As you may imagine, I'm too lazy to test it - basically because I don't want to write an example file :)

    OFF-TOPIC Update: note that your node had been considered in order to remove the ------------------------------------------------ line at the beginning of the script. This is due to the fact that in Unix-like environments having #!/usr/bin/perl at the very beginning (I mean, starting from the first character) allows automatic selection of the interpreter. You can edit and remove them by yourself :)

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
      The redundancy can be eliminated:
      #!/usr/bin/perl my $MaxLineLength = 8060; $search = '<COLUMNS>'; $replace = '<DATA>'; $searchEnd = '</COLUMNS>'; $replaceEnd = '</DATA>'; $fileContents = ''; my $linePrefix = ''; while (<STDIN>) { chomp; # Remove newline if (/(^<DATA>\t).*(<\/DATA>$)/) { next if length() > $MaxLineLength; } elsif (/(^<COLUMNS>\t).*(<\/COLUMNS>$)/) { s/$search/$replace/g; s/$searchEnd/$replaceEnd/g; } else { next; } print $linePrefix; # Empty only for first line print; $linePrefix = "\n"; }
        Thanks to all for your help!
Re: Newline Problem
by Tanktalus (Canon) on Jun 30, 2005 at 15:04 UTC

    Untested...

    #!/usr/bin/perl my $MaxLineLength = 8060; $search = '<COLUMNS>'; $replace = '<DATA>'; $searchEnd = '</COLUMNS>'; $replaceEnd = '</DATA>'; $fileContents = ''; my $first = 0; while (<STDIN>) { chomp; # print "\n" if $first++; if (/(^<DATA>\t).*(<\/DATA>$)/) { if (length() <= $MaxLineLength) { print "\n" if $first++; print; } } elsif (/(^<COLUMNS>\t).*(<\/COLUMNS>$)/) { s/$search/$replace/g; s/$searchEnd/$replaceEnd/g; print "\n" if $first++; print; } }

    The key is to print out a newline at the beginning of each input line, unless it's the very first line, and also to remove the newline from the input (via chomp) so we don't print it out later.

    Update: Code update as per frodo72's comments - that'll learn me for reading the question without reading the code.

      Note that you're actually printing a lot of (probably) unwanted newlines, one for every rejected line. You should move the
      print "\n" if $first++;
      just before every following print; statement.

      Flavio
      perl -ple'$_=reverse' <<<ti.xittelop@oivalf

      Don't fool yourself.