Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Print all lines in a file except the last two

by Ryszard (Priest)
on May 20, 2004 at 14:56 UTC ( [id://354967]=note: print w/replies, xml ) Need Help??


in reply to Print all lines in a file except the last two

Assuming a CR after each line:
1 2 3 4 1234567890123456789012345678901234567890123456789 perl -e 'open(F,'f');@f=<F>;pop@f;pop@f;print"$_"foreach@f'

Replies are listed 'Best First'.
Re: Re: Print all lines in a file except the last two
by hardburn (Abbot) on May 20, 2004 at 16:41 UTC

    You can remove the pops and change the print to print@f[0..$#f-2] for a savings of 13 strokes.

    ----
    send money to your kernel via the boot loader.. This and more wisdom available from Markov Hardburn.

Re: Re: Print all lines in a file except the last two
by Jasper (Chaplain) on May 20, 2004 at 15:50 UTC
    Why a counter and imply it's short when you have a foreach, and a print"$_" ?

    print for@f[0..-2] is shorter.

    Edit: Wow, I really do live in a little fluffy bunny world where everyone's happy, there's no war, pizza isn't fattening, Darth Vader was never an annoying child, the Six million Dollar Man really was a good tv show, new shoes are comfy, my girlfriend's bum never looks big in this, and code like this works.

    Kind of surprised I didn't lose all the xp I'd ever gotten on this one. hardburn posted the correct code below.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://354967]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2024-03-28 10:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found