2013-03-20 112 views
2

这一个让我。Perl循环没有打印出字符

这工作:

print "Processing Feed\n"; 
while (my @p = $mainex->fetchrow_array) { 
    my $iis = "$pcount"; 
    print "$iis\n"; 
    # ... Do Other Stuff Here 
    $pcount++; 
} 

其中给出:

Processing Feed 
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
... 

这不起作用(除去从第4行的\ N):

print "Processing Feed\n"; 
while (my @p = $mainex->fetchrow_array) { 
    my $iis = "$pcount"; 
    print "$iis"; 
    # ... Do Other Stuff Here 
    $pcount++; 
} 

它只是给出了:

Processing Feed 

我试图建立一个计数器,将其输出高达使用类似的记录的计数:

while(Something){ 
    print "\b\b\b\b\b\b\b\b\b\b\b"; 
    print "$count"; 
    $count++; 
    # Do stuff here 
} 

任何想法,为什么当出现在第二个例子中没有任何\ n被打印到屏幕?之前我已经做了很多次,并且不知道为什么它不起作用。

+0

您不需要强制'$ count'作为[print] $ count“;'作为['print'](http://p3rl.org/print”perldoc -f print“)的字符串。迫使它充当一个字符串。所以只需写'print $ count;'。至于'print'$ iis \ n“;'vs'print $ iis,”\ n“;在这种情况下,真的没关系。 – 2013-03-22 01:33:56

回答

6

打印结束处的换行符会触发stdout的刷新,并将其打印到屏幕上。如果您将$|++添加到您的perl脚本的顶部,它将打开标准输出的自动填充功能,您将看到您的号码。

+0

真棒谢谢! – someuser 2013-03-20 18:09:35

+0

我没有得到的唯一原因是为什么这个工程然后: – someuser 2013-03-20 18:12:17

+0

$ i = 0; ($ i!= 1000){ print“。”; } – someuser 2013-03-20 18:12:39

4

缓冲I/O。

当有换行符或缓冲区已满时(可能是512字节或4096字节或其他相当大的数字),数据会刷新到屏幕上。