Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

printing - Why aren't newlines being printed in this Perl code?

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) . " 
";
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

Therefore, what you need is:

print( ($val / 8050) . "
" );

or

print +($val / 8050) . "
";

The statement you have prints the result of $val / 8050 and then concatenates " " to the return value of print and then discards the resulting value.

Incidentally, if you:

use warnings;

then perl will tell you:

   print (...) interpreted as function at t.pl line 5.
   Useless use of concatenation (.) or string in void context at t.pl line 5.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...