Perl 编程/练习 2 答案
外观
< Perl 编程
1. 答案
print "Input a line of text: "; $text = <STDIN>; print "\n"; print "You entered: $text\n";
2. 答案
my @lines; for (my $i=0; $i<3; $i++) { print "Input a line of text:\n"; chomp(my $line = <STDIN>); $lines[$i] = $line; } $" = "|"; print "@lines\n";
chomp
在这里有什么用?尝试在没有它的情况下运行程序。
3. 答案
my $line1 = readline STDIN; my $line2 = readline STDIN; my $line3 = readline STDIN; chomp($line1, $line2, $line3); print("| $line1 | $line2 | $line3 |\n");
答案
foreach (@ARGV) { print "$_\n"; }
答案
open my $file, "<", "foo.txt"; my @lines; foreach (<$file>) { chomp($_); push(@lines, $_); } $" = "|"; print "@lines\n";