in reply to Newline Problem

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.

Replies are listed 'Best First'.
Re^2: Newline Problem
by ikegami (Patriarch) on Jun 30, 2005 at 16:15 UTC
    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!