in reply to reading a random line from a file
Just jump to a very random position via seek() and then read the next line you find:
open my $fh,'<',$some_file or die $!; my $size = -s $fh; seek $fh, rand($size) , 0; <$fh>; #throw away current line; my $randomline = <$fh>; close $fh;
Only problem is that you might seek() to a position in the last line of the file and will get no random line. But it's not too hard to solve this problem.
--
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: reading a random line from a file
by sauoq (Abbot) on Nov 17, 2002 at 22:48 UTC |