Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Welcome to the Monastery
 
PerlMonks  

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

by Ryszard (Priest)
on May 20, 2004 at 14:56 UTC ( #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'


Comment on Re: Print all lines in a file except the last two
Download Code
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.
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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (9)
As of 2014-04-21 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (495 votes), past polls