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
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. |