[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Random lines from a File



On Thu, 4 Mar 1999 17:14:42 -0500, Chris Nandor wrote:

>At 16.13 -0500 1999.03.04, Ronald J. Kimball wrote:
>>But a disimprovement in that it has to read the whole file five times over!
>
>Yep.  To fix that is an exercise left to the reader.

Exercise time!

$file = shift; @seek = ();
open(F,$file);
# preparation:
while($seek = tell(F), defined($_=<F>)) {
	push @seek, $seek;
}

#go!
sub randomline {
	seek F,$seek[int(rand @seek)],0;
	return scalar <F>;
}

print &randomline, &randomline, &randomline;

	Bart.

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org