Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: How do I read 5 lines from a file that's read from a directory that's being listed

by grep (Monsignor)
on Dec 21, 2001 at 01:11 UTC ( [id://133614]=note: print w/replies, xml ) Need Help??


in reply to How do I read 5 lines from a file that's read from a directory that's being listed

Well to make it easier on me, I turned this into a command line function. Should be easy to convert back.
The ideas were solid, but with some improvements you can make this more solid and much easier to debug problems like this.
#!/usr/bin/perl -w #use CGI; # you should be using CGI.pm #use strict 'nuff said use strict; #You should factor out the directory name since you are using it in mu +ltiple places; my $HOMEDIR = '.'; #you should put the name of the file and a newline for errors (much ea +sier to understand) #you should avoid '||', use 'or' instead. 'or' will be what want most +of the time. # FYI '||' binds tighter than 'or; opendir(HOMEDIR, "$HOMEDIR") or die ("Unable to open directory $HOMEDI +R\n"); my @files = grep !/^\./, readdir(HOMEDIR); closedir(HOMEDIR); #You should learn to use $_, saves loads of typing foreach (reverse sort @files) { #check to see if it is actually a file (you don't want directories +) next if (!(-f "$HOMEDIR/$_")); print "--->$_<---\n"; #same as above open(STUFF, "./$_") or die ("Unable to open file $_\n"); while(<STUFF>) { 1 .. 5 ? print: last; } close (STUFF); print "\n"; }
HTH
grep

grep> cd pub
grep> more beer
  • Comment on Re: How do I read 5 lines from a file that's read from a directory that's being listed
  • Download Code

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found