Я бы сделал что-то вроде:
#!/usr/bin/perl
use strict;
use warnings;
use Data::Dump qw(dump);
my $quest;
my %QA;
while(my $line = <DATA>) {
chomp $line;
if ($line =~ /^\((\d+)\)$/) {
$quest = $1;
$QA{$quest} = [];
} elsif ($line =~ /\S+/ && defined $quest) {
push @{$QA{$quest}}, $line;
}
}
dump %QA;
__DATA__
Junk
Junk
Junk
(1)
How is the weather in India?
A Good
B Bad
C Terrible
(2)
Are you hungry right this second?
True
False
(3)
Which is a fruit?
A Africa
B China
C India
D Asia
E America
F Apple
G Mexico
(4)
A mystery is?
A Game
B Problem
C I don't know
D Nothing
Выход:
(
4,
[
"A mystery is?",
"A Game",
"B Problem",
"C I don't know",
"D Nothing",
],
1,
["How is the weather in India?", "A Good", "B Bad", "C Terrible"],
3,
[
"Which is a fruit?",
"A Africa",
"B China",
"C India",
"D Asia ",
"E America",
"F Apple",
"G Mexico",
],
2,
["Are you hungry right this second?", "True", "False"],
)