Removing the first or last lines from a text file

If you've ever run into a situation where you know you won't be needing those pesky first or last lines in a text file (this also works for other arrays, BTW), then this is a quick and easy way to get just what you're looking for:

Explanation: you can call a particular object by the index, or in this case, a particular line in the file.  Remember, the index number will be one number lower than the line number, because the index starts at 0, so line 20 will be at index 19, or $test[19].

You can also call a range of indices, such as to get lines 1 through 5, you would use $test[0..4].  You can use a variable number to specify the index as well, such as $test[$index].

Put this all together, and you can strip the last line out of your variable with:

Since the count is going to be the full number of lines, $var.count - 1 would be the last line, so you want it to stop at the next to last line, or $var.count - 2.

Removing the first line is likewise simple, with just a tiny modification:

If you need more than one line off of either side, just tweak the numbers.  Simple!