EDIT: I think I finally understand the question:
The OP has a file which, for lack of better terminology, contains questions and responses. Questions always come before responses. Both types of statements are enclosed in double quotes. There is a blank line (i.e. "\n\n"
) between a question and its associated response. The OP wants to read the questions and their associated responses one-by-one (not line-by-line).
There are several approaches to this (without necessarily slurping). One is to assume that double-quotes do not appear anywhere other than at the beginning or end of the strings of interest. I am not sure how valid an assumption this is which makes the following script fragile. Note that the last block is invalid because the answer is not enclosed in double quotes.
#!/usr/bin/perl
use strict;
use warnings;
while (
defined(my $q = read_statement(\*DATA))
and defined(my $a = read_statement(\*DATA))
) {
print "QUESTION: $q\nANSWER: $a\n\n";
}
sub read_statement {
my ($fh) = @_;
my $line;
while ( $line = <$fh> ) {
last if $line =~ /^"/;
}
return unless defined $line;
return $line if $line =~ /"$/;
my $statement = $line;
while ($line = <$fh> ) {
$statement .= $line;
last if $line =~ /"$/;
}
return unless $statement =~ /"$/;
return $statement;
}
Test input:
__DATA__
"Hi how are you?"
"Hello
im
fine, thank you!"
"How is the weather?"
"It rained
all week.
It's been
gray
and cold since the 15th"
"Who are you?"
Sinan
Output:
C:\Temp> t
QUESTION: "Hi how are you?"
ANSWER: "Hello
im
fine, thank you!"
QUESTION: "How is the weather?"
ANSWER: "It rained
all week.
It's been
gray
and cold since the 15th"