in reply to truncating a text file

Maybe I'm missing something, but isn't it as simple as this?
$str = 'line 5<br>line 4<br>line 3<br>line 2<br>line 1<br>'; @lines = split('<br>',$str); unshift (@lines,'line 6'); $output = join('<br>',@lines[0..4]); print $output;


Nobody says perl looks like line-noise any more
kids today don't know what line-noise IS ...

Replies are listed 'Best First'.
Re^2: truncating a text file
by ikegami (Patriarch) on Jul 17, 2007 at 14:24 UTC

    That gives warnings and extra <br>s when there aren't five lines yet.

    >perl -we"@lines = qw( a b c ); print join '<br>', @lines[0..4]; Use of uninitialized value in join or string at -e line 1. Use of uninitialized value in join or string at -e line 1. a<br>b<br>c<br><br>

    Also, given $html_doc = "a new set - line 1 {br} $html_doc", you're short a br when there are five lines.

      i think the code should be as follows, to prevent the warnings
      $str = 'line 5<br>line 4<br>line 3<br>line 2<br>line 1<br>'; @lines = split('<br>',$str); unshift (@lines,'line 6'); $output = join('<br>',@lines); print $output;
      the hardest line to type correctly is: stty erase ^H
        Removing the line that gives a warning is a great way of stopping it from giving the warning, as long as you don't mind that the program no longer does the one thing the OP asked for help with.