in reply to Re: Split a variable at a Given Line-Number
in thread Split a variable at a Given Line-Number

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.

Replies are listed 'Best First'.
Re^3: Split a variable at a Given Line-Number
by LanX (Saint) on May 18, 2010 at 13:36 UTC
    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".

        Granted, but you should care about trailing empty lines.

        Cheers Rolf

        UPDATE:

        for completeness ($pre,$post)=split /((?:\n.*){16}$)/,$txt