如何通过随机选择解析一组给定问题?
您好,我正在编写一个小型解析脚本,最好用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会做类似的事情:
输出:
I'd do something like:
Output:
我已经找到了一种方法来剥离所有问题并将它们放入哈希中。现在我需要存储与它们相对应的选项。
太好了,答案是,问题后面的行就是答案,所以一旦找到问题,只需继续附加您看到的行,直到达到 1 )一个新问题或 2)空行
更新:如果您不打算自己进行错误检查,
使用 autodie;
简单版,找到一个题号,其他都算内容
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