home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.perl
- Path: sparky!uunet!fmrco!fmrco!asherman
- From: asherman@laser.fmrco.com (Aaron Sherman)
- Subject: Re: how to get passwd
- In-Reply-To: lwall@netlabs.com's message of 3 Sep 92 18:43:43 GMT
- Message-ID: <ASHERMAN.92Sep4130521@laser.fmrco.com>
- Sender: news@fmrco.uucp
- Organization: I-Kinetics, 19 Bishop-Allen Dr., Cambridge, MA
- References: <23093@hacgate.SCG.HAC.COM> <ASHERMAN.92Sep2110720@laser.fmrco.com>
- <1992Sep3.184343.12651@netlabs.com>
- Distribution: usa
- Date: Fri, 4 Sep 1992 18:05:21 GMT
- Lines: 67
-
-
- >>>>> lwall@netlabs.com (Larry Wall) said:
-
- lwall> How many times a month are you going to be doing this? 1928347 times?
- lwall> 100 times? 3 times?
-
- I don't understand what this has to do with it. Are you saying that
- efficiency is not important because you don't have to do it very often
- (fair enough in many cases) or that what I wrote was less efficient
- than calling tail (see below)?
-
- lwall> How 'bout:
- lwall> $uid = (split(/:/, `tail -1 /etc/passwd`))[2];
-
- I would add a 0 to the result to force it to be a number. $uid eq ''
- can cause some things to choke (unless you use $uid as a number later
- on anyway).
-
- lwall> Yes, you've got a Swiss Army Chainsaw. It's not required, even in the
- lwall> Swiss Army, that you open your marmelade [sic] with it.
-
- I'll admit that this is a much simpler way of doing things (perhaps
- faster, depends on how tail does things. I seem to remember that tail
- reads the whole file), but I was replying to a question that
- specifically asked how to do it in perl, so I wasn't thinking along
- those lines at all. (This seems sort of like trying to explain why I
- would use EMACS to Richard Stallman...)
-
- As to my posting, though... I'm convinced (only by my gut) that the
- way that I used seek to find the last line is not the fastest or most
- efficient way to find the last line. My main goal was to NOT read
- through the whole file, but I think that my algorithm for finding the
- last line (seek -100 from EOF, then read all 100 chars, then seek back
- 200, etc) was less efficient than it could be. How do you "read a file
- backwards"? That was what I wanted, but I didn't want to call seek
- between each getc.
-
- What's faster (and/or smaller and/or more efficient) than this:
-
- $\="\n";
- open(F,'</etc/passwd') || die("Cannot open /etc/passwd: $!\n");
-
- if (seek(F,-100,2))
- {
- do
- {
- $i=0;
- while(defined($c=getc(F)) && $c ne "\n")
- {
- last unless ($i++ < 100);
- }
- } while($c ne "\n" && seek(F,-200,1));
- }
-
- seek(F,0,0) if (tell < 100);
- $last = $_ while(<F>);
- print((split(/:/,$last))[2]+0);
-
-
-
- -AJS
-
- --
- --------
- Disclaimer: I am solely responsible for the content of this message.
- The views expressed here may not be the views of I-Kinetics, Fidelity,
- any of the Fidelity-owned corporations or my mother.
-