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", $_;
	}
RSS 2.0 | leave a response | trackback

4 Responses

  1. 一 14th, 2009 / 3:58 下午 bonnie Says:

    lz,我测试怎么你第二导体的答案是错误的哦

    [回复]

  2. 一 15th, 2009 / 10:29 上午 latteye Says:

    @bonnie
    我又运行了一下,没有看到错误呢。我跑下来结果是这样的:向右侧靠齐,宽度为20个字符。
    大于20个字符的继续向右侧延伸。

    [回复]

  3. 十 26th, 2009 / 5:09 下午 1x1 Says:

    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 回复:

    第一行是 my @test = ;

    [回复]

发表评论