如何通过随机选择解析一组给定问题?

发布于 2024-12-09 07:57:58 字数 1179 浏览 1 评论 0原文

您好,我正在编写一个小型解析脚本,最好用 Perl 来解析一组给定的问题。总是有四个问题(现在用于测试目的)。它们的格式如下:

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

所以模式是相同的,数字(1)在前,然后是空格,后面是问题。现在答案可能会有所不同,我正在尝试找到一种方法来保持一致。我找到了一种方法来剥离所有问题并将它们放入哈希中。现在我需要存储与它们相对应的选择。

我的代码现在可以将上面的内容剥离到一个具有以下内容的数组中:

PRINTING ARRAY OF Q'S 
How is the weather in India?
Are you hungry right this second?
Which is a fruit?
A mystery is?

代码是:

#! /usr/bin/perl
use strict;
use warnings;

open (FILE, "$ARGV[0]" );



my @questions = ();

my $flagToCapture = 0 ;
my $foundQuestion = 0 ;

while (<FILE>){

if ($flagToCapture ==2 ) {
    $flagToCapture = 0;
    chomp($_);
    push(@questions, $_);
    #inserted question into the array
    $foundQuestion = 1;
}

if ($flagToCapture == 1){
    $flagToCapture = 2;
}

if ($_ =~ m/^\(/) {
    $flagToCapture =1;
}

}

print "PRINTING ARRAY OF Q'S \n";
foreach(@questions){
    print "$_\n";
}

Hi I am working on a small parsing script, preferably in Perl to parse a given set of questions. There are always four questions (for testing purposes right now). They are formatted as follows:

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

So the pattern is the same, the number (1) comes first, then a blank space followed by the question. Now the answer can choices can vary and I am trying to figure out a way to keep it consistent. I've figured out a way to strip all the questions and place them into a hash. Now I need to store the choices that correspond with them.

My code right now can strip the contents above into an array that has the following:

PRINTING ARRAY OF Q'S 
How is the weather in India?
Are you hungry right this second?
Which is a fruit?
A mystery is?

Code is :

#! /usr/bin/perl
use strict;
use warnings;

open (FILE, "$ARGV[0]" );



my @questions = ();

my $flagToCapture = 0 ;
my $foundQuestion = 0 ;

while (<FILE>){

if ($flagToCapture ==2 ) {
    $flagToCapture = 0;
    chomp($_);
    push(@questions, $_);
    #inserted question into the array
    $foundQuestion = 1;
}

if ($flagToCapture == 1){
    $flagToCapture = 2;
}

if ($_ =~ m/^\(/) {
    $flagToCapture =1;
}

}

print "PRINTING ARRAY OF Q'S \n";
foreach(@questions){
    print "$_\n";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯叶蝶 2024-12-16 07:57:58

我会做类似的事情:

#!/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"],
)

I'd do something like:

#!/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

Output:

(
  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"],
)
半世晨晓 2024-12-16 07:57:58

我已经找到了一种方法来剥离所有问题并将它们放入哈希中。现在我需要存储与它们相对应的选项。

太好了,答案是,问题后面的行就是答案,所以一旦找到问题,只需继续附加您看到的行,直到达到 1 )一个新问题或 2)空行

更新:如果您不打算自己进行错误检查,使用 autodie;
简单版,找到一个题号,其他都算内容

use autodie;
...
my %questions;
my $questionNumber = '';
while (<FILE>){
    if( /^\s* \( (\d+) \) \s* $/x ){
        $questionNumber = $1;
        $questions{ $questionNumber } = '';
    } else {
        $questions{ $questionNumber } .= $_;
    }
}
while( my( $k, $v ) = each %questions ){
    print "($k)$v\n";
    print Dumper( $k => QuestionToHash( $v ) );
}

I've figured out a way to strip all the questions and place them into a hash. Now I need to store the choices that correspond with them.

Great, the answer is, the lines following the questions are the answers, so once you find a question, simply keep append the lines you see, until you reach 1)a new question or 2) blank line

update: if you're not going to do the error checking yourself, use autodie;
the simple version, find a question number, everything else is considered content

use autodie;
...
my %questions;
my $questionNumber = '';
while (<FILE>){
    if( /^\s* \( (\d+) \) \s* $/x ){
        $questionNumber = $1;
        $questions{ $questionNumber } = '';
    } else {
        $questions{ $questionNumber } .= $_;
    }
}
while( my( $k, $v ) = each %questions ){
    print "($k)$v\n";
    print Dumper( $k => QuestionToHash( $v ) );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文