为什么在此Perl代码中不打印换行符? [英] Why aren't newlines being printed in this Perl code?
本文介绍了为什么在此Perl代码中不打印换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一些简单的Perl代码:
I have some simple Perl code:
#!/usr/bin/perl
use strict; # not in the OP, recommended
use warnings; # not in the OP, recommended
my $val = 1;
for ( 1 .. 100 ) {
$val = ($val * $val + 1) % 8051;
print ($val / 8050) . " \n";
}
但是当我运行它时,输出为:
But when I run it, the output is:
bash-3.2$ perl ./rand.pl
0.0002484472049689440.000621118012422360.003229813664596270.08409937888198760.92
... <snipped for brevity> ...
2919250.9284472049689440.3526708074534160.1081987577639750.2295652173913040.1839
751552795030.433540372670807bash-3.2$
我做错什么了吗?
推荐答案
C:\>
perldoc -f print
:
也请注意不要遵循 带左括号的print关键字 除非你想要相应的 右括号终止 打印参数-插入+ 或在所有 争论.
Also be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print--interpose a + or put parentheses around all the arguments.
因此,您需要的是:
print( ($val / 8050) . "\n" );
或
print +($val / 8050) . "\n";
您拥有的语句打印$val / 8050
的结果,然后将"\n"
连接到print
的返回值,然后丢弃结果值.
The statement you have prints the result of $val / 8050
and then concatenates "\n"
to the return value of print
and then discards the resulting value.
顺便说一句,如果您:
use warnings;
然后perl
会告诉您:
print (...) interpreted as function at t.pl line 5.
Useless use of concatenation (.) or string in void context at t.pl line 5.
这篇关于为什么在此Perl代码中不打印换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文