Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl STDIN在while循环之前执行_Perl_While Loop_Stdin - Fatal编程技术网

Perl STDIN在while循环之前执行

Perl STDIN在while循环之前执行,perl,while-loop,stdin,Perl,While Loop,Stdin,我是个十足的perl noob,希望这个问题不要太愚蠢 我正在尝试编写一个类似于刽子手的游戏,并让它全部正常工作,除了出于某种原因,STDIN似乎在while循环的其余部分之前执行——因此,在打印任何内容之前,我不得不输入所有的字母猜测 while() { foreach $letter (@currentword) { print "$letter"; } print "\n"; print "Guess a letter: \n";

我是个十足的perl noob,希望这个问题不要太愚蠢

我正在尝试编写一个类似于刽子手的游戏,并让它全部正常工作,除了出于某种原因,STDIN似乎在while循环的其余部分之前执行——因此,在打印任何内容之前,我不得不输入所有的字母猜测

while()
{
    foreach $letter (@currentword)
    {
        print "$letter";
    }
    print "\n";
    print "Guess a letter: \n";
    chomp($guess = <STDIN>);
    $guess=lc(substr($guess,0,1));  
    if ( $randword =~ /($guess)+/i )
    {
        print "Correct!\n";
        for ($index=0; $index<$wordlength; $index++)
        {
            if ($wordarray[$index] eq $guess)
            {
                $currentword[$index]=$wordarray[$index];
            }
        }       
    }
    else
    {
        print "Incorrect! try again\n";
    }   
    $guesses -= 1;
    last if $guesses < 1;
    $finalword = join("",@currentword);
    last if ($finalword eq $randword);  
}
while()
{
foreach$字母(@currentword)
{
打印“$字母”;
}
打印“\n”;
打印“猜一个字母:\n”;
chomp($guess=);
$guess=lc(substr($guess,0,1));
如果($randword=~/($guess)+/i)
{
打印“正确!\n”;

对于($index=0;$index您的标准输出必须连接到终端以外的其他对象,从而导致使用块缓冲而不是行缓冲。请尝试添加

$| = 1;

(不过,我认为从标准输入中读取数据会导致标准输出被刷新。)

在将
$randword
$wordlength
@wordarray
@currentword
$guesses
初始化为合理值后,此代码工作正常。您能给我们展示更多的代码吗?特别是在您打开STDIN的地方?虽然不完全理解这行代码的功能,但它可以工作!我会去做更多的阅读和分析找到哈哈,谢谢!关闭当前选定文件句柄(STDOUT)的缓冲。请参阅。