in reply to Extract random records from text file
I set $/ to undef, and read the whole thing in, splitting it on your delimeter into @questions. I then randomly remove (splice) questions out of the array until it contains the number of questions you want...sub get_std_test { die +(caller(0))[3].' cannot be called in void context' if not defined wantarray; local $/; # set our delimiter to nothing, slurp file my %config = mcas::get_config(); # returns a hash of all config vars my @questions; open QUESTIONS, 'questions' or die "Cannot open QUESTIONS file: $!"; my @questions = split "===\n", <QUESTIONS>; close QUESTIONS; while(@questions > $config{num_questions_per_test}) { splice @questions, int(rand(@questions)), 1; } die "\$count = $count, < num_questions_per_test config var" if (@questions < $config{num_questions_per_test}); return @questions; }
- Ant
- Some of my best work - Fish Dinner
|
|---|