2014-10-22 2 views
1

나는 일종의 퀴즈 프로그램을 작성 중입니다. 나는 테스트 은행으로 .txt 파일을 사용하고 있지만 (정규 표현식을 사용하여) 각 질문을 일치시키고 다른 라인에서 가능한 답을 출력하는 방법을 알아낼 수 없다. 원래는 진정한 거짓을 수행하려고했기 때문에 일치시킬 필요가 없었다. 다른 것은 "1"과 잘 어울립니다. 기본적으로 나는 한 줄로 된 질문과 다른 질문에 대한 답을 필요로합니다. 난 정말 당신이에서지고 있었다 무엇을 얻을 수 없었다, 그래서이 샘플 프로그램을 작성정규식 일치 후 선행 인쇄

while (<$test>) { 

    foreach my $line (split /\n/) { 

     my $match1 = "1"; 

      if ($line =~ /$match1/) { 
       $question1 = $line; 
       print "$question1\n"; 
       print "Answer: ";   
       $answer1 = <>; 
       chomp ($answer1); 
        if ($answer1 =~ /(^a$)/i) { 
         $score1 = 20; 
          push @score, $score1; 
        }  
      } 
+0

<$test> 동안 만 당신에게 한 줄을 줄 것입니다. – sln

+2

@sln,'$ /'이 무엇인지에 달려 있습니다. 그것이 정의되지 않은 경우, 당신은 파일을 slurp. 그러나 while 루프에서 파일을 버리지는 않습니다. 그러나 특정 레코드 분리 기호를 사용할 수 있습니다. – Axeman

+0

그 다음 그는 그 동안 필요하지 않습니다(). 그가 $ test를 close/open에서 재사용하지 않는 한 split보다 아래에있다. 예, 사실입니다. 이름이 새 라인이 아닌 어설픈 구분 기호를 사용할 수 있습니다. – sln

답변

1

: 여기에 전에 내가 한 질문

1.) some text 
a.) solution 
b.) solution 
c.) solution 

코드의 예입니다.

use 5.016; 
use strict; 
use warnings; 
my (@lines, @questions, $current_question); 

sub prompt { 
    my ($prompt) = @_; 
    print $prompt, ' '; 
    STDOUT->flush; 
    my $val = <>; 
    return $val; 
} 

QUESTION: 
while (<DATA>) { 
    if (my ($ans) = m/^=(\w+)/) { 
     INPUT: { 
      say @lines; 
      last unless defined(my $answer = prompt('Your answer:')); 
      say ''; 
      my ($response) = $answer =~ /([a-z])\s*$/; 
      if (not $response) { 
       $answer =~ s/\s*$//; #/ 
       say "Invalid response. '$answer' is not an answer!\n"; 
       redo INPUT; 
      } 
      if ($response eq $ans) { 
       say 'You are right!'; 
      } 
      elsif (my $ansln = $current_question->{$response}) { 
       if ($response eq 'q') { 
        say 'Quitting...'; 
        last QUESTION; 
       } 
       say <<"END_SAY"; 
You chose:\n$current_question->{$response} 
The correct answer was:\n$current_question->{$ans} 
END_SAY 
      } 
      else { 
       say "Invalid response. '$response' is not an answer!\n"; 
       redo INPUT; 
      } 
     }; 
     @lines =(); 
     prompt('Press enter to continue.'); 
     say ''; 
    } 
    else { 
     if (my ($qn, $q) = m/^\s*(\d+)\.\)\s+(.*\S)\s*$/) { 
      push @questions, $current_question = { question => $q }; 
     } 
     else { 
      my ($l, $a) = m/^\s+([a-z])/; 
      $current_question->{$l} = (m/(.*)/)[0]; 
     } 
     push @lines, $_; 
    } 
} 

__DATA__ 
1.) Perl is 
    a.) essential 
    b.) fun 
    c.) useful 
=c 
2.) This question is 
a.) Number two 
b.) A test to see how this format is parsed. 
c.) Unneeded 
=b 
0

이것은 아마도 단순화 된 것입니다.
그냥 테스트 데이터를 읽고 구조를 만듭니다.
시험 응시자들의 답변에 점수를 매기는 데 사용할 수 있습니다.

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' 
           } 
       } 
     };