[Tutor] Re: [Tutor]Reading and Writing (No Arithmetic?) [linecache?]

Blake Winton bwinton@tor.dhs.org
Sat, 19 Jan 2002 08:28:12 -0500


* Danny Yoo <dyoo@hkn.eecs.berkeley.edu> [020118 22:32]:
> On Fri, 18 Jan 2002, Paul Sidorsky wrote:
> To get a random line from a file still requires that we count how many
> lines are in a file, but that can be a quick linear scan.

Alternately, couldn't we just go to a random byte in the file, scan
backwards for the first newline, then forwards for the next newline,
and that would give us a random line?

I guess it would tend to weight longer lines heavier, but I don't
think that's a terrible restriction, given that it should be
stupid-fast, and tke up very little memory.

Perhaps I'll whip up some code to do this later...

Later,
Blake.
-- 
9:40pm up 52 days, 21:07, 2 users, load average: 0.02, 0.09, 0.07