in reply to Extract random records from text file
my $q_file = "tmp.txt"; my $n = 10; my @ques = get_questions($q_file, $n); print for @ques; sub get_questions { local $/="===\n"; local $.; # Is this necessary? local @ARGV = (shift); # If you want 'open ... or die' behavior local $SIG{__WARN__} = sub { die shift }; my $num = shift; my @questions; while (<>) { push(@questions, $_), next if $. <= $num; my $i = rand($.); $questions[$i] = $_ if $i < $num; } @questions; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 2 (unrandomness): Extract random records from text file
by tilly (Archbishop) on Oct 02, 2001 at 22:15 UTC | |
by runrig (Abbot) on Oct 02, 2001 at 23:34 UTC | |
by tilly (Archbishop) on Oct 03, 2001 at 21:30 UTC |