Monks, I am faced with a task of reading a text file line by line, then analyzing each line. The "reading" part of the file by line is easier, but the "analyzing" part is what troubles me.
This is a sample of my text file:
1. What is my name?
A. Rooney *
B. Gerrard
C. Ronaldo
D. Ronaldinho
2. Who's your bet in the coming World Cup?
A. Brazil *
B. Germany
C. England
D. Cameroon
My code for reading each line goes like this:
use FileHandle;
$fh = new FileHandle;
$fh->open("<c:\\test.txt") || die ("Could not open file!!!!. $!");
until ($fh->eof) {
$tmp = $fh->getline;
# If $tmp starts with any number followed by a period (it means that
+ the part after the period is the question portion), do FUNCTION1
# If $tmp starts with a "letter" followed by a period (it means that
+ this is one of the choices), do FUNCTION2
}
$fh->close;
In the above example, if:
$tmp = "1. What is my name?"
How will I match the "number" until it encounters a "period", discard this portion and assigns the remaining part to:
$question = "What is my name?"
Same goes for this also:
$tmp = "A. Rooney *"
How will I match the "letter" until it encounters a "period", discards this portion and assigns the remaining part to:
$choice_a = "Rooney *"
Also if it encounters "*" at the end of the choices, it removes this "*" and declares this as the answer:
Example:
$choice_a = "Rooney *" (becomes $choice_a = "Rooney" after "*" is removed)
$choice_b = "Gerrard"
$choice_c = "Ronaldo"
$choice_d = "Ronaldinho"
$answer = "Rooney"
Please bear with me as I am not good is regular expressions, matching, etc.
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.