I am currently developing a generic web-based multiple choice testing system backend for a road safety project I'm currently working on. The system, Multiple Choice Assessment System (MCAS), has already given me a lot of problems (but BTW thanks to arturo, clemburg, mattr and Chmrr who have already helped me out a lot!).

All the questions are stored in a simple text file. A sample question looks like this:

question=When driving towards a bright setting sun, glare can be reduc +ed by option1=closing one eye option2=dipping in the interior mirror option3=wearing dark glasses option4=looking sideways answer=4 answertext=Dark glasses are often used as a fashion item but they do h +ave their practical uses. Low sun in the early morning or evening can + dazzle and distract. Lessen the risk by reducing the glare. Wear dar +k glasses if they help you to see better. category1=car category2=vechicle handling
Questions in the file are delimited by \n===\n and each question may have a variable number of lines (as number of options can vary and some questions have 'visual aids' etc.)

The idea is that each test has n questions (n is specified in a config file). A script is run on the main questions file that produces a preset number of 'test files'. When the system is eventually ready to 'go live', a random 'test file' is selected and converted to HTML to be displayed to the user.

What I'm having trouble with now is my make_tests.pl script that, as the name implies :-) creates the 'test files'. It is meant to randomly select n questions from the main questions file and save them as a 'test file'. Thanks partially to the above monks and partially to 'The Perl Cookbook', I know have the following code (that is intended to return a random list of n questions):

sub get_std_test { die +(caller(0))[3].' cannot be called in void context' if not defined wantarray; $/ = "===\n"; # set our delimiter my %config = mcas::get_config(); # returns a hash of all config vars my $count = 0; my @questions; open QUESTIONS, '<questions' or die "Cannot open QUESTIONS file: $!" +; while (<QUESTIONS>) { if (rand($.) < 1) { if ($count == $config{num_questions_per_test}) { last; } else { push @questions, $_; $count++ } } } close QUESTIONS; die "\$count = $count, < num_questions_per_test config var" if ($count < $config{num_questions_per_test}); return @questions; }
The function currently seems to return only lines, not whole questions. I think clemburg in particular might have warned me about this, and said that I needed some kind of 'read_record' routine but I've got no idea how to go about this (in particular, how to 'maintain state' or maintain file position between different invocations of the function).

If anyone can offer any advice (or even code!), I'd greatly appreciate it.


In reply to Extract random records from text file by SuperCruncher

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.