blog » Dev # 写写代码 » Perl 语言入门 第五章
Perl 语言入门 第五章
先把自己拼拼凑凑写出来的东西贴出来,找到了英文版本 CHM,看过那个里面的答案。简单几行代码就能看出自己的代码组织能力很差。回头再分析一下原文答案。
5.1
1 2 3 | #!/usr/bin/perl -w print <>; |
5.2
1 2 3 4 5 6 7 8 | #!/usr/bin/perl -w #while (defined ($_=<STDIN>)){ while (<STDIN>){ printf "123456789012345678901234567890\n"; chomp ($_); printf "%20s\n", $_;} |
5.3
1 2 3 4 5 6 7 8 9 10 11 | #!/usr/bin/perl -w #while (defined ($_=<STDIN>)){ print "Please enter width:"; $width=<STDIN>; chomp ($width); while (<STDIN>){ printf "123456789012345678901234567890\n"; chomp ($_); printf "%${width}s\n", $_; } |
lz,我测试怎么你第二导体的答案是错误的哦
[回复]
@bonnie
我又运行了一下,没有看到错误呢。我跑下来结果是这样的:向右侧靠齐,宽度为20个字符。
大于20个字符的继续向右侧延伸。
[回复]
5-3这样是不是更好些?
my @test = ;
chomp(my $count = shift @test);
my $num = $count;
if($count > 35){$count = 35;}
while($count >= 10){
print “0123456789″;
$count -= 10;
}
if($count){print 0 .. ($count-1);}
print “\n”;
foreach(@test){
chomp($_);
printf “%${num}s\n”, $_;
}
[回复]
1x1 回复:
十月 26th, 2009 at 5:10 下午
第一行是 my @test = ;
[回复]