Looks like you're on the right track, I would suggest using some more Perlish syntax ... for instance rather than print and exit when the open fails, use die and make sure to print the $! variable because it will contain why it died. Also you're using a while loop to put the file into an array but Perl can do that for you automagically. Finally alway use strict and possible warnings as they will help you to debug.

I would probably tackle it like this:

use strict; use warnings; "print "This program will ask the user random questions from a file un +til all the questions have been answered.\n"; open(QUESTFILE, 'questions.txt') or die "Couldn't open questions.txt: +$!\n"; my @questions = <QUESTFILE>; # slurps up the entire file close(QUESTFILE);

Before I continue, your answer file confuses me... opening it with > overwrites it. And it looks like you are trying to read from it but then you don't do anything with its contents that I can see. I'm going to assume you just want to append the answers.

# this opens the answers file for append open(ANSFILE,">>answers.txt") or die "Couldn't open answers for append +ing: $!\n"; # rather than a while loop and a variable just use a foreach # loop wi +th a range ... $_ will contain the number foreach(1 .. 10) { print $questions[ int( rand( scalar(@questions) ) )]; my $ans = uc(<STDIN>); print ANSFILE $ans; } close(ANSFILE); exit;

The only thing about this code is that it has the potential to ask the same question twice. You might use a hash to keep track of which questions you've already asked but I'll leave that as a excersise for you :)

Hope that helps!
Chris

Lobster Aliens Are attacking the world!

In reply to Re: Random lines from a file for a quiz by cfreak
in thread Random lines from a file for a quiz by Anonymous Monk

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.