Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: how to print first 5 lines of a file

by CharlesClarkson (Curate)
on Nov 01, 2001 at 03:42 UTC ( [id://122455]=note: print w/replies, xml ) Need Help??


in reply to how to print first 5 lines of a file

for (1 .. 5) { print scalar <>; last if eof; }

Or, if you're certain there are at least five lines:

print scalar <> for 1 .. 5;

Calling the diamond operator <> in scalar context will return 1 line, assuming $/ is set to "\n". (Note - 'while' calls in scalar context while 'foreach' and 'for' call in list context.)



HTH,
Charles K. Clarkson

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-20 11:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found