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

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!cs.utexas.edu!convex!convex!tchrist
  3. From: Tom Christiansen <tchrist@convex.COM>
  4. Subject: Re: # of files in directory
  5. Message-ID: <1992Jul23.225715.21531@news.eng.convex.com>
  6. Originator: tchrist@pixel.convex.com
  7. Keywords: Files, Perl, Directory, ls
  8. Sender: usenet@news.eng.convex.com (news access account)
  9. Nntp-Posting-Host: pixel.convex.com
  10. Reply-To: tchrist@convex.COM (Tom Christiansen)
  11. Organization: CONVEX Realtime Development, Colorado Springs, CO
  12. References: <1992Jul23.195931.20791@magnus.acs.ohio-state.edu> <1992Jul23.201539.4416@news.eng.convex.com> <1992Jul23.211249.21744@magnus.acs.ohio-state.edu>
  13. Date: Thu, 23 Jul 1992 22:57:15 GMT
  14. X-Disclaimer: This message was written by a user at CONVEX Computer
  15.               Corp. The opinions expressed are those of the user and
  16.               not necessarily those of CONVEX.
  17. Lines: 65
  18.  
  19. From the keyboard of bobd@magnus.acs.ohio-state.edu (Bob DeBula):
  20. :In article <1992Jul23.201539.4416@news.eng.convex.com> tchrist@convex.COM (Tom Christiansen) writes:
  21. :>From the keyboard of bobd@magnus.acs.ohio-state.edu (Bob DeBula):
  22. :>:To reduce it still further (this *is* PERL after all):
  23. :>:
  24. :>:opendir (FILE, "/usr/bin");
  25. :>:print "There are ", scalar(grep (!/^\./, readdir (FILE))), " files!\n";
  26. :>:
  27. :>:Now if I could just figure out how to embed the open in the print
  28. :>:statement.....
  29. :>
  30. :>print "There are ", scalar grep(1,`ls /usr/bin/`), " files in /usr/bin.\n";
  31. :                   ^^^^^^^^^^^^^^
  32. :                   But, that would 
  33. :                   be *cheating* :-)
  34. :                
  35. :
  36. :Actually, how does this compare in efficiency to the "opendir"
  37. :method? (My timings would seem to indicate that the "opendir" form
  38. :is slightly (almost infintesimally) faster on my Sun 3/80).
  39.  
  40. Right -- depends on your system and the phase of the moon.
  41.  
  42. On mine, opendir() is method 1, `ls` is method 2, and I ran 100
  43. iterations:
  44.  
  45.     method 1:   1.1500u   0.2333s
  46.     method 2:   1.0667u   0.6833s
  47.  
  48. :While the goal is to get it down to one line, you must color
  49. :in the lines (stay within PERL that is :-). So, can you do it
  50. :in one line *without* using any semi-colons or backquoting
  51. :to use external utilities? 
  52.  
  53. Surely.  
  54.  
  55.     print "There are ", 
  56.         scalar(opendir(F,"/usr/bin"),scalar(grep(!/^\./,readdir(F)))), 
  57.     " files!\n"
  58.  
  59. You can join these into one line if you'd like.
  60.  
  61. :Think of it as one of those penultimately
  62. :pointless PERL challenges which crop up from time to time :-)
  63.  
  64. Sigh, pretty as it sounds, I lament to inform you that penultimate doesn't
  65. mean more than ultimate.  Ultimate often means last in modern dialects of
  66. Latin.  Penultimate means second to the last, and antepenultimate means
  67. third to the last.
  68.  
  69. :Hmmm, "Penultimately Pointless PERL Programming Problem" sounds
  70. :like some sort of invitational named by Robin Leach, doesn't it? 
  71. :What would we give for a prize to the winner of the 1st PPPPP 
  72. :Invitational? Maybe we could get Larry to donate some "suitable 
  73. :for framing" original napkins with PERL patches scribbled on them 
  74. :(complete with obligatory coffee stains)?
  75.  
  76. But I do like the idea.
  77.  
  78. --tom
  79. -- 
  80.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  81.     Besides, including <std_ice_cubes.h> is a fatal error on machines that
  82.     don't have it yet.  Bad language design, there...  :-)
  83.             --Larry Wall in <1991Aug22.220929.6857@netlabs.com>
  84.