想要在控制台中打印为输出。

huangapple go评论69阅读模式
英文:

want to print the in console as an output

问题

这是我的示例代码,我想将报告打印在控制台上作为输出,我尝试了很多次,但无法做到,我是新学习者。

#!/usr/local/bin/perl

# for循环执行
$b=0;
for( $a = 0; $a < 5; $a = $a + 1 ) {

   print "请输入您的姓名:" ;
   my $name = <STDIN>;
   chomp $name;
   open(my $report, '>', 'report.txt');
   close $report;

}
英文:

This is my sample code I want to print the report in the console as an output, I tried too many times but I unable to do this, new learner.

#!/usr/local/bin/perl

# for loop execution
$b=0;
for( $a = 0; $a &lt; 5; $a = $a + 1 ) {

   print &quot;Enter your name please&quot; ;
   my $name = &lt;STDIN&gt;;
   chomp $name;
   open(my $name, &#39;&gt;&#39;, &#39;report.txt&#39;);
   close $name;

}

答案1

得分: 2

以下是您要翻译的内容:

my $qfn = 'report.txt';

open(my $fh, '<', $qfn)
   or die("无法打开 \"$qfn\": $!\n");

while (<$fh>) {
   print;
}

最后三行等同于

while ($_ = <$fh>) {
   print($_);
}

所以您也可以使用

while (my $line = <$fh>) {
   print($line);
}
英文:
my $qfn = &#39;report.txt&#39;;

open(my $fh, &#39;&lt;&#39;, $qfn)
   or die(&quot;Can&#39;t open \&quot;$qfn\&quot;: $!\n&quot;);

while (&lt;$fh&gt;) {
   print;
}

The last three lines are equivalent to

while ($_ = &lt;$fh&gt;) {
   print($_);
}

So you could also use

while (my $line = &lt;$fh&gt;) {
   print($line);
}

答案2

得分: 1

我认为OP正在要求将控制台输出打印到报告中。

#!/usr/local/bin/perl
# for循环执行

$b = 0; 现在这是不需要的标量变量声明

for ($a = 0; $a < 5; $a = $a + 1) { # 这里可以使用$a++而不是$a = $a + 1;
    print "请输入您的姓名:";
    my $name = <STDIN>;
    chomp $name; # 你将检查文件处理I/O。
    open($filename, '>>', 'report.txt'); # 你需要将值追加到报告中
    print $filename "$name\n";
    close $filename;
}

只需尝试这个
英文:

I think the OP is asking to print the console output into the report.

#!/usr/local/bin/perl
# for loop execution

$b=0; Now this is unwanted scalar variable declaration

   for( $a = 0; $a &lt; 5; $a = $a + 1 ) { # You can use $a++ here instead of $a = $a + 1 ;
   print &quot;Enter your name please&quot; ;
   my $name = &lt;STDIN&gt;;
   chomp $name; #You will check the filehandling I/O.
   open($filename, &#39;&gt;&gt;&#39;, &#39;report.txt&#39;); #You need to append the values into the report
   print $filename &quot;$name\n&quot;;
   close $filename;
}

Just try this.

答案3

得分: 1

根据提供的代码,您从标准输入读取并输出到 report.txt 文件中。请注意,您的脚本包含了一个shebang,表示您在使用Linux/UNIX操作系统。

#!/usr/local/bin/perl

use strict;
use warnings;

my $report = 'report.txt';
my $a = 5;

open(my $fh, '>', $report)
    or die "Couldn't open $report";

while( $a-- ){
    print "Enter your name please: " ;
    my $name = <STDIN>;
    chomp $name;

    say $fh $name;

}

close $fh;

希望对您有所帮助。

英文:

Judging by provided piece of code you read from STDIN and output to report.txt file

#!/usr/local/bin/perl

use strict;
use warnings;

my $report = &#39;report.txt&#39;;
my $a = 5;

open(my $fh, &#39;&gt;&#39;, $report)
	or die &quot;Couldn&#39;t open $report&quot;;

while( $a-- ){
	print &quot;Enter your name please: &quot; ;
	my $name = &lt;STDIN&gt;;
	chomp $name;
	
	say $fh $name;

}

close $fh;

NOTE: your script includes shebang what indicates that you use Linux/UNIX

答案4

得分: 1

这个回答适用于如果 OP 想要在 Perl 中实现 tee Linux/UNIX 命令提供的功能。Man 页面 tee

在使用该脚本之前,应将其设置为可执行文件 chmod +x script.pl

该命令将继续将内容打印到终端,并将相同的输出保存到提供的 filename 或默认文件 report.txt

用法:command | script.pl [filename]

注意:旧文件会被覆盖

#!/usr/local/bin/perl

use strict;
use warnings;

use feature 'say';

my $report = shift || 'report.txt';

open(my $fh, '>', $report)
    or die "Couldn't open $report";

while( <> ){
    say;
    say $fh $_;
}

close $fh;
英文:

This answer applies to the case if OP wants to implement in perl feature provided by tee Linux/UNIX command. Man page tee.

Prior usage of the script it should be made executable chmod +x script.pl.

The command will continue to print into the terminal and save same output into file with provided filename or otherwise default file report.txt.

USAGE: command | script.pl [filename]

NOTE: old file gets overwritten

#!/usr/local/bin/perl

use strict;
use warnings;

use feature &#39;say&#39;;

my $report = shift || &#39;report.txt&#39;;

open(my $fh, &#39;&gt;&#39;, $report)
	or die &quot;Couldn&#39;t open $report&quot;;

while( &lt;&gt; ){
	say;
	say $fh $_;
}

close $fh;

huangapple
  • 本文由 发表于 2020年1月6日 15:05:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/59607969.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定