in reply to Split a variable at a Given Line-Number

Untested:
use List::Util qw(max); my @lines = split /\n/, $str; my $prefix = join "\n", @lines[0..@lines-16]; my $postfix = join "\n", @lines[max(0, @lines-15)..$#lines];

(Update: fixed offby1 error)

Replies are listed 'Best First'.
Re^2: Split a variable at a Given Line-Number
by LanX (Saint) on May 18, 2010 at 12:55 UTC
    slicing lists instead of arrays and using negative indices is even shorter:

    $txt=join "\n",0..99; print join "\n", (split /\n/, $txt, -1 )[-5 ..-1];

    OUT

    95 96 97 98 99

    Cheers Rolf

    UPDATE:

    This handles the case of #slicelines > #textlines

    print join "\n",grep {defined} (split /\n/,$txt,-1 )[-16 ..-1]

    UPDATE: added -1 to handle trailing empty lines, but I suppose thats not intended in this usecase.

      Hole-in-one! :)

      print ( (split /(\n)/,$txt )[-32..-1] )

      Cheers Rolf

      Update: added parens for print...

        To quote the original problem:
        variable into two separate variables where one variable contains the last 15 lines of text (including any newline characters) and the other contains the rest.

        (emphasis by me). Somehow I fail to see two separate variables in your "solution".

Re^2: Split a variable at a Given Line-Number
by danj35 (Sexton) on May 18, 2010 at 13:40 UTC
    No problems with that as of yet, works absolutely fine. Never seen max before, so thanks for showing me it. Cheers.