Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Note: I'm a beginner.

I have a file with questions (lines ending with a "?") followed immediately with answers, then a new line break:

$ cat quiz.txt Name 2 shapes? square circle Which continent is north of South America? North America Who was the 1st person on the moon? Neil Armstrong

I'm trying to create a perl script that prompts me with the question, followed by my <STDIN> and then displays the answer/s. Just as simple as that. No evaluation of whether the answer is correct and no score measurements etc..

I've done a perl one-liner that can prompt me the questions successfully:

$ perl -le '@array_q = `cat quiz.txt | grep ?`; foreach(@array_q){ print ; chomp($enter=<STDIN>) ; print "\n";}'

I then did another one-liner that can probe me for the answer:

$ perl -le '@array_ans = `cat quiz.txt | grep -v ?`; foreach(@array_ans){ print ; chomp($enter=<STDIN>) ; print "\n";}'

Since I had the 2 components of my quiz engine, I thought it would be simple combining the 2 one-liners using a nested foreach loop, but I'm running into all sorts of problems:

perl -le '@array_ans = `cat quiz.txt | grep -v ?`; foreach(@array_ans){ @array_q = `cat quiz.txt | grep ?`; foreach(@array_q){ print ; chomp($enter=<STDIN>) ; print "\n";}  print;}'

The output is all over the place and not what I expected.

Now I'm not necessarily interested in the answer, but more if I'm thinking about this problem in the correct way. After chewing on this issue for a week, I'm thinking along the lines of hashes being more appropriate since a quiz question can be associated with an answer. Any advice please?

In reply to How could I create a command line quiz? by yasser

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 or How to display code and escape characters are good places to start.
Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2022-08-20 02:30 GMT
Find Nodes?
    Voting Booth?

    No recent polls found