I have some simple Perl code:
我有一些简单的Perl代码:
#!/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$
Am I doing something wrong?
难道我做错了什么?
3 个解决方案
#1
C:\>
perldoc -f print
:
C:\> perldoc -f 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关键字,除非你想要相应的右括号来终止print的参数 - 插入一个+或在所有参数周围加上括号。
Therefore, what you need is:
因此,您需要的是:
print( ($val / 8050) . "\n" );
or
print +($val / 8050) . "\n";
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.
您打印的语句是$ val / 8050的结果,然后将“\ n”连接到print的返回值,然后丢弃结果值。
Incidentally, if you:
顺便说一下,如果你:
use warnings;
then perl
will tell you:
然后perl会告诉你:
print (...) interpreted as function at t.pl line 5. Useless use of concatenation (.) or string in void context at t.pl line 5.
#2
This is more of a comment than an answer, but I don't know how else to make it and the question is already answered anyway.
这更像是一个评论而不是答案,但我不知道如何做到这一点,无论如何问题已经得到了回答。
Note that using say instead of print neatly sidesteps the whole issue. That is,
请注意,使用say而不是print来整齐地回避整个问题。那是,
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
my $val = 1;
for ( 1 .. 100 ) {
$val = ($val * $val + 1) % 8051;
say ($val / 8050);
}
works as intended without the issue even coming up. I'm still amazed at how useful say is, given it's such a tiny difference.
按计划工作,即使没有出现问题。鉴于它有如此微小的差异,我仍然对这说有多有用感到惊讶。
#3
It is possible that the line is interpreted as follows
该线可能解释如下
(print($val / 8050)) . " \n";
i.e. the parentheses being used as delimiters for a function argument list, with the ."\n" being silently discarded. Try:
即括号被用作函数参数列表的分隔符,而“。\ n”被静默丢弃。尝试:
print( ($val/8050) . "\n" );
#1
C:\>
perldoc -f print
:
C:\> perldoc -f 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关键字,除非你想要相应的右括号来终止print的参数 - 插入一个+或在所有参数周围加上括号。
Therefore, what you need is:
因此,您需要的是:
print( ($val / 8050) . "\n" );
or
print +($val / 8050) . "\n";
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.
您打印的语句是$ val / 8050的结果,然后将“\ n”连接到print的返回值,然后丢弃结果值。
Incidentally, if you:
顺便说一下,如果你:
use warnings;
then perl
will tell you:
然后perl会告诉你:
print (...) interpreted as function at t.pl line 5. Useless use of concatenation (.) or string in void context at t.pl line 5.
#2
This is more of a comment than an answer, but I don't know how else to make it and the question is already answered anyway.
这更像是一个评论而不是答案,但我不知道如何做到这一点,无论如何问题已经得到了回答。
Note that using say instead of print neatly sidesteps the whole issue. That is,
请注意,使用say而不是print来整齐地回避整个问题。那是,
#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
my $val = 1;
for ( 1 .. 100 ) {
$val = ($val * $val + 1) % 8051;
say ($val / 8050);
}
works as intended without the issue even coming up. I'm still amazed at how useful say is, given it's such a tiny difference.
按计划工作,即使没有出现问题。鉴于它有如此微小的差异,我仍然对这说有多有用感到惊讶。
#3
It is possible that the line is interpreted as follows
该线可能解释如下
(print($val / 8050)) . " \n";
i.e. the parentheses being used as delimiters for a function argument list, with the ."\n" being silently discarded. Try:
即括号被用作函数参数列表的分隔符,而“。\ n”被静默丢弃。尝试:
print( ($val/8050) . "\n" );