| [reply] |
If your HTML is not terribly complex, you could use something like YAML
use strict;
use warnings;
use Data::Dumper;
use YAML;
# Load a YAML stream of 3 YAML documents into Perl data structures.
my $data = do {local $/ = <DATA> };
my @yaml = Load( $data );
for my $hash (@yaml) {
my $question = $hash->{question};
my $answer = $hash->{answer};
print "q: $question\na: $answer\n";
}
__DATA__
---
question: <b>What... is your name?</b>
answer: <p>My name is Sir Lancelot of Camelot.</p>
---
question: <b>What... is your quest?</b>
answer: <p>To seek the Holy Grail.</p>
---
question: <b>What... is your favourite colour?</b>
answer: <p>Blue.</p>
| [reply] [d/l] [select] |
my ($question, $answer) = split /\? /, $line;
| [reply] [d/l] |
| [reply] |
| [reply] |
As you will undoubtedly hear, TMTOWTDI = There’s More Than One Way To Do It.” I rather like to do things as simply, and as obviously, as I can.
The first thought that comes to mind is that you could use a loop which reads the input one string at a time, chomps it (to remove newlines), then appends it to a string variable. Now, within that loop, we have another while loop that tries to find question-marks within that string buffer. While it is able to do so, it grabs the first part of the buffer string (ending with the question-mark) as its output, then assigns the remainder of that string back to the string-buffer ... repeating this process until there are no more substrings left to be “eaten.”
If the content of the string buffer contains no question marks, the inner while loop will of course execute zero times.
At the end of these two nested while loops, the string buffer variable may be defined (but it won’t be, if the file was completely empty), and if so, it may contain a non-empty string, which is the detrius at the end of the file if the file didn’t end in a question-mark.
I have decided not to provide code, because I think you want to figure it out on your own. “Use two nested while loops ...” “TMTOWTDI.”
| |