이것은 아마도 단순화 된 것입니다.
그냥 테스트 데이터를 읽고 구조를 만듭니다.
시험 응시자들의 답변에 점수를 매기는 데 사용할 수 있습니다.
use strict;
use warnings;
use Data::Dumper;
$/ = undef;
my $testdata = <DATA>;
my %HashTest =();
my $hchoices;
my $hqeustion;
my $is_question = 0;
while ($testdata =~ /(^.*)\n/mg)
{
my $line = $1;
$line =~ s/^\s+|\s+$//g;
next if (length($line) == 0);
if ($line =~ /^(\d+)\s*\.\s*\)\s*(.*)/)
{
$is_question = 1;
$HashTest{ $1 }{'question'} = $2;
$HashTest{ $1 }{'choices'} = {};
$HashTest{ $1 }{'answer'} = 'unknown';
$hqeustion = $HashTest{ $1 };
$hchoices = $HashTest{ $1 }{'choices'};
}
elsif ($is_question && $line =~ /^\s*(answer)\s*:\s*([a-z])/)
{
$hqeustion->{'answer'} = $2;
}
elsif ($is_question && $line =~ /^\s*([a-z])\s*\.\s*\)\s*(.*)/)
{
$hchoices->{ $1 } = $2;
}
}
print "\nQ & A summary\n-------------------------\n";
for my $qnum (keys %HashTest)
{
print "Question $qnum: $HashTest{$qnum}{'question'}'\n";
my $ans_code = $HashTest{$qnum}{'answer'};
print "Answer: ($ans_code) $HashTest{$qnum}{'choices'}{$ans_code}\n\n";
}
print "---------------------------\n";
print Dumper(\%HashTest);
__DATA__
1.) What is the diameter of the earth?
a.) Half the distance to the sun
b.) Same as the moon
c.) 6,000 miles
answer: c
2.) Who is buried in Grants Tomb?
a.) Thomas Edison
b.) Grant, who else
c.) Jimi Hendrix
answer: b
출력 :
Q & A summary
-------------------------
Question 1: What is the diameter of the earth?'
Answer: (c) 6,000 miles
Question 2: Who is buried in Grants Tomb?'
Answer: (b) Grant, who else
---------------------------
$VAR1 = {
'1' => {
'question' => 'What is the diameter of the earth?',
'answer' => 'c',
'choices' => {
'c' => '6,000 miles',
'a' => 'Half the distance to the sun',
'b' => 'Same as the moon'
}
},
'2' => {
'question' => 'Who is buried in Grants Tomb?',
'answer' => 'b',
'choices' => {
'c' => 'Jimi Hendrix',
'a' => 'Thomas Edison',
'b' => 'Grant, who else'
}
}
};
출처
2014-10-22 22:51:30
sln
<$test> 동안 만 당신에게 한 줄을 줄 것입니다. – sln
@sln,'$ /'이 무엇인지에 달려 있습니다. 그것이 정의되지 않은 경우, 당신은 파일을 slurp. 그러나 while 루프에서 파일을 버리지는 않습니다. 그러나 특정 레코드 분리 기호를 사용할 수 있습니다. – Axeman
그 다음 그는 그 동안 필요하지 않습니다(). 그가 $ test를 close/open에서 재사용하지 않는 한 split보다 아래에있다. 예, 사실입니다. 이름이 새 라인이 아닌 어설픈 구분 기호를 사용할 수 있습니다. – sln