Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Shelling out to cat or grep in backticks is almost never what you want to do (unless you're doing a one-liner throwaway or something). And your approach loses the correspondence between questions and answers.

You should write a sub, let's call it parse_quiz_text, which will take the filename and return your questions and answers. The sub can cheat and use local $/ = q{}; (see perlvar) to say you want to read in paragraph mode, then you'd split off the first line from the answer(s). Each question would be represented by a hashref of the question and an arrayref of answers (perldsc and perlref will be useful if you're unfamiliar with those).

sub parse_quiz_text { my( $quiz_file ) = @_; open( my $fh, q{<}, $quiz_file ) or die "problem opening quiz '$quiz +_file': $!\n"; local( $/ ) = q{}; my @quiz_questions; while( defined( my $paragraph = <$fh> ) ) { my @lines = split( /\n/, $paragraph ); push @quiz_questions, { question => (shift @lines), answers => \@l +ines }; } close( $fh ); return @quiz_questions; }

Additionally: caveat that this does no error checking and is presuming the file is in the correct format (e.g. questions are always the first line of a paragraph, questions are always followed by at least one answer, yadda yadda yadda). You'd probably want to add some error checking at some point (check that the first line of the paragraph ends in a '?', check that @lines >= 2, . . .).

The cake is a lie.
The cake is a lie.
The cake is a lie.


In reply to Re: How could I create a command line quiz? by Fletch
in thread How could I create a command line quiz? by yasser

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2024-03-28 18:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found