home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / lang / perl / 4886 < prev    next >
Encoding:
Text File  |  1992-07-22  |  1.3 KB  |  38 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!gatech!destroyer!sol.ctr.columbia.edu!The-Star.honeywell.com!umn.edu!mmm.serc.3m.com!web!timbuk.cray.com!hemlock.cray.com!roehrich
  3. From: roehrich@cray.com (Dean Roehrich)
  4. Subject: Re: # of files in directory
  5. Message-ID: <1992Jul22.150613.26006@hemlock.cray.com>
  6. Keywords: Files, Perl, Directory, ls
  7. Lines: 26
  8. Organization: Cray Research, Inc.
  9. References: <1992Jul22.182940.1248@chpc.org>
  10. Date: 22 Jul 92 15:06:13 CDT
  11.  
  12. In article <1992Jul22.182940.1248@chpc.org> shishir@chpcchpc.org (Shishir Gundavaram) writes:
  13. >Can anyone please tell me if there an easy way in Perl to determine
  14. >the number of files in a particular directory.
  15. >
  16. >One way I can think of to do this is:
  17. >
  18. >    opendir (FILE, "/usr/bin");
  19. >    @some_array = grep (!/^\./, readdir (FILE));
  20. >    $number = $#some_array + 1;
  21. >    print "There are $number of files!";
  22.  
  23.  
  24. You can throw out the $number stuff.  Remember that $#array gives you the
  25. subscript of the last element while scalar( @array ) gives you the length of
  26. the array.  (Also, you did not compensate for $[.)
  27.  
  28. Replace your last 2 lines with:
  29.     print "There are ", scalar @some_array, " files!\n";
  30.  
  31. If you're looking for speed, use 'while' rather than 'grep'.  The code may
  32. look more mundane but on a large directory it'll probably blow your grep
  33. away.
  34.  
  35.  
  36. Dean Roehrich
  37. roehrich@cray.com
  38.