Perl 重新运行循环迭代
我有一个程序,它接受一个单词数组,并要求用户键入一个句子,其中包含数组中的每个单词:Perl 重新运行循环迭代,perl,loops,Perl,Loops,我有一个程序,它接受一个单词数组,并要求用户键入一个句子,其中包含数组中的每个单词: @words = qw(Hi world thanks); foreach $word (@words) { print "Enter a line with word:$word\n"; chomp($input = <STDIN>); if($input=~/$word/i) { print "G
@words = qw(Hi world thanks);
foreach $word (@words)
{
print "Enter a line with word:$word\n";
chomp($input = <STDIN>);
if($input=~/$word/i)
{
print "Great\n";
} else {
print "Incorrect. Type a line containing $word\n";
}
}
@words=qw(你好,谢谢大家);
foreach$word(@words)
{
打印“输入一行文字:$word\n”;
chomp($input=);
如果($input=~/$word/i)
{
打印“Great\n”;
}否则{
打印“不正确。键入包含$word的行\n”;
}
}
如果用户用这个词输入,它就可以正常工作。但如果他不这样做,它只会打印错误信息并移动到下一个单词。我希望它能提示用户重新输入同一单词的输入。但是怎么做呢?我尝试了下一步,但它不起作用。在这种情况下,您可以使用a来重新启动当前迭代
我将创建一个无限
while
循环以退出:
#!/usr/bin/env perl
use strict;
use warnings;
my @words = qw(Hi world thanks);
foreach my $word (@words) {
print "Enter a line with word: $word\n";
while (1) {
chomp( my $input = <STDIN> );
if( $input=~/$word/i ) {
print "Great\n";
last;
} else {
print "Incorrect. Type a line contaning $word\n";
}
}
}
#/usr/bin/env perl
严格使用;
使用警告;
我的@words=qw(你好,谢谢你);
foreach我的$word(@words){
打印“输入一行文字:$word\n”;
而(1){
chomp(我的$input=);
如果($input=~/$word/i){
打印“Great\n”;
最后;
}否则{
打印“不正确。键入包含$word的行\n”;
}
}
}
当然,我可能会将每个单词的逻辑分成一个子词,然后循环:
#!/usr/bin/env perl
use strict;
use warnings;
my @words = qw(Hi world thanks);
get_word($_) for @words;
sub get_word {
my $word = shift or die "Need a word";
print "Enter a line with word: $word\n";
while (1) {
chomp( my $input = <STDIN> );
if( $input=~/$word/i ) {
print "Great\n";
last;
} else {
print "Incorrect. Type a line contaning $word\n";
}
}
}
#/usr/bin/env perl
严格使用;
使用警告;
我的@words=qw(你好,谢谢你);
获取@words的单词($);
分词{
我的$word=shift或die“需要一句话”;
打印“输入一行文字:$word\n”;
而(1){
chomp(我的$input=);
如果($input=~/$word/i){
打印“Great\n”;
最后;
}否则{
打印“不正确。键入包含$word的行\n”;
}
}
}
而重做
无疑更可爱,下面是一个带有而。。。继续
。它依赖于一个内部循环,该循环仅在输入正确的单词时退出,并为每个错误答案打印一个更正
use strict;
use warnings;
my @words = qw(Hi world thanks);
foreach my $word (@words) {
print "Enter a line with word: $word\n";
while (my $input = <>) {
last if $input =~ /$word/;
} continue {
print "Incorrect. Type a line contaning $word\n";
}
print "Great\n";
}
使用严格;
使用警告;
我的@words=qw(你好,谢谢你);
foreach我的$word(@words){
打印“输入一行文字:$word\n”;
while(my$input=){
如果$input=~/$word/,则为最后一个;
}继续{
打印“不正确。键入包含$word的行\n”;
}
打印“Great\n”;
}
请注意,在这种情况下不需要chomp
天哪,14票赞成重做?很长一段时间以来,我一直在perl标签中看到如此巨大的赞誉。我想在任何一个标签中都不需要chomp
,只是因为正则表达式没有锚定,而且它不是一个eq
比较。@JoelBerger因此是“在这种情况下”。哦,我以为你是指在你的情况下。Gotcha@JoelBerger我想最好的做法是包含chomp
,以防以后有人试图将代码用于其他需要chomp的东西。我刚刚得到了简化代码的东西。
#!/usr/bin/env perl
use strict;
use warnings;
my @words = qw(Hi world thanks);
get_word($_) for @words;
sub get_word {
my $word = shift or die "Need a word";
print "Enter a line with word: $word\n";
while (1) {
chomp( my $input = <STDIN> );
if( $input=~/$word/i ) {
print "Great\n";
last;
} else {
print "Incorrect. Type a line contaning $word\n";
}
}
}
use strict;
use warnings;
my @words = qw(Hi world thanks);
foreach my $word (@words) {
print "Enter a line with word: $word\n";
while (my $input = <>) {
last if $input =~ /$word/;
} continue {
print "Incorrect. Type a line contaning $word\n";
}
print "Great\n";
}