perlquestion
yasser
<p> Note: I'm a beginner. </p>
<p> I have a file with questions (lines ending with a "?") followed immediately with answers, then a new line break: </p>
<p>
<code>$ 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
</code>
</p>
<p> 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.. </p>
<p> I've done a perl one-liner that can prompt me the questions successfully: </p>
<code>$ perl -le '@array_q = `cat quiz.txt | grep ?`; foreach(@array_q){ print ; chomp($enter=<STDIN>) ; print "\n";}' </code>
<p> I then did another one-liner that can probe me for the answer: </p>
<p>
<code>$ perl -le '@array_ans = `cat quiz.txt | grep -v ?`; foreach(@array_ans){ print ; chomp($enter=<STDIN>) ; print "\n";}' </code></p>
<p>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: </p>
<code>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;}' </code>
<p>The output is all over the place and not what I expected.</p>
<p>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?</p>