home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / perl / 5729 < prev    next >
Encoding:
Text File  |  1992-09-04  |  2.7 KB  |  82 lines

  1. Newsgroups: comp.lang.perl
  2. Path: sparky!uunet!fmrco!fmrco!asherman
  3. From: asherman@laser.fmrco.com (Aaron Sherman)
  4. Subject: Re: how to get passwd
  5. In-Reply-To: lwall@netlabs.com's message of 3 Sep 92 18:43:43 GMT
  6. Message-ID: <ASHERMAN.92Sep4130521@laser.fmrco.com>
  7. Sender: news@fmrco.uucp
  8. Organization: I-Kinetics, 19 Bishop-Allen Dr., Cambridge, MA
  9. References: <23093@hacgate.SCG.HAC.COM> <ASHERMAN.92Sep2110720@laser.fmrco.com>
  10.     <1992Sep3.184343.12651@netlabs.com>
  11. Distribution: usa
  12. Date: Fri, 4 Sep 1992 18:05:21 GMT
  13. Lines: 67
  14.  
  15.  
  16. >>>>> lwall@netlabs.com (Larry Wall) said:
  17.  
  18. lwall> How many times a month are you going to be doing this?  1928347 times?
  19. lwall> 100 times?  3 times?
  20.  
  21. I don't understand what this has to do with it. Are you saying that
  22. efficiency is not important because you don't have to do it very often
  23. (fair enough in many cases) or that what I wrote was less efficient
  24. than calling tail (see below)?
  25.  
  26. lwall> How 'bout:
  27. lwall>     $uid = (split(/:/, `tail -1 /etc/passwd`))[2];
  28.  
  29. I would add a 0 to the result to force it to be a number. $uid eq ''
  30. can cause some things to choke (unless you use $uid as a number later
  31. on anyway).
  32.  
  33. lwall> Yes, you've got a Swiss Army Chainsaw.  It's not required, even in the
  34. lwall> Swiss Army, that you open your marmelade [sic] with it.
  35.  
  36. I'll admit that this is a much simpler way of doing things (perhaps
  37. faster, depends on how tail does things. I seem to remember that tail
  38. reads the whole file), but I was replying to a question that
  39. specifically asked how to do it in perl, so I wasn't thinking along
  40. those lines at all. (This seems sort of like trying to explain why I
  41. would use EMACS to Richard Stallman...)
  42.  
  43. As to my posting, though... I'm convinced (only by my gut) that the
  44. way that I used seek to find the last line is not the fastest or most
  45. efficient way to find the last line. My main goal was to NOT read
  46. through the whole file, but I think that my algorithm for finding the
  47. last line (seek -100 from EOF, then read all 100 chars, then seek back
  48. 200, etc) was less efficient than it could be. How do you "read a file
  49. backwards"? That was what I wanted, but I didn't want to call seek
  50. between each getc.
  51.  
  52. What's faster (and/or smaller and/or more efficient) than this:
  53.  
  54.     $\="\n";
  55.     open(F,'</etc/passwd') || die("Cannot open /etc/passwd: $!\n");
  56.     
  57.     if (seek(F,-100,2))
  58.     {
  59.         do
  60.         {
  61.         $i=0;
  62.         while(defined($c=getc(F)) && $c ne "\n")
  63.         {
  64.             last unless ($i++ < 100);
  65.         }
  66.         } while($c ne "\n" && seek(F,-200,1));
  67.     }
  68.         
  69.     seek(F,0,0) if (tell < 100);
  70.     $last = $_ while(<F>);
  71.     print((split(/:/,$last))[2]+0);
  72.  
  73.  
  74.  
  75.             -AJS
  76.  
  77. --
  78. --------
  79. Disclaimer: I am solely responsible for the content of this message.
  80. The views expressed here may not be the views of I-Kinetics, Fidelity,
  81. any of the Fidelity-owned corporations or my mother.
  82.