What have you tried so far? Monks tend to offer more help to people who are trying to solve a problem, versus people looking to have the community write their code for them.
Good places to start would be open and split.
The basic flow of the program should be something like this:
- Open the file
- Read each line, splitting the text into article and noun.
- Find the total number of entries
- Generate a random number between 1 and the last index of the set
- Loop, Printing the noun and testing user input against the article
A few hints:
Open a file:
open my $fh, '<', $filename or die "Failed to open $filename - $!";
Read the file one line at a time:
while (my $line = <$fh>) {
# Do something with $line
}
Check the number of items in a list (by evaluating a list in scalar context):
my $count = @list;
Get a line of input:
my $answer = <STDIN>;
Update - hint: always use strict and warnings
Always use the following at the top of your script:
use strict;
use warnings;
By default perl is extremely forgiving and tries to do what you mean. Sometimes this is desireable, but normally you want perl to be strict and catch the mistakes you make. Save yourself a lot of debugging time by always using both strict and warnings.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.