LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 960|回复: 7

请教:关于输出格式?

[复制链接]
发表于 2004-4-28 15:07:47 | 显示全部楼层 |阅读模式
刚看完循环,做的练习~,望指教 :thank
[php]
#!perl -w
#
for($i=1;$i<=9;$i++){
    @a=(1..9);
    print join(' ',(map{$_*=$i}(@a))),"\n";
    @a=();
}
[/php]结果
1 2 3 4 5 6 7 8 9
2 4 6 8 10 12 14 16 18
3 6 9 12 15 18 21 24 27
4 8 12 16 20 24 28 32 36
5 10 15 20 25 30 35 40 45
6 12 18 24 30 36 42 48 54
7 14 21 28 35 42 49 56 63
8 16 24 32 40 48 56 64 72
9 18 27 36 45 54 63 72 81
请问如何把输出的结果对齐?
发表于 2004-4-28 16:28:39 | 显示全部楼层

右对齐

[PHP]
#!/usr/bin/perl

for($i=1;$i<=9;$i++){
    my @a=(1..9);
    my @out=(map{$_*=$i}(@a));
        foreach (@out) {
                printf("%4d",$_);
        }
        print "\n";
}
[/PHP]
或者
[PHP]
#!/usr/bin/perl
for($i=1;$i<=9;$i++){
    my @a=(1..9);
    my @out=(map{$_*=$i}(@a));
format STDOUT =
@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>
$out[0],$out[1],$out[2],$out[3],$out[4],$out[5],$out[6],$out[7],$out[8]
.
        write;
}

[/PHP]

  1.    1   2   3   4   5   6   7   8   9
  2.    2   4   6   8  10  12  14  16  18
  3.    3   6   9  12  15  18  21  24  27
  4.    4   8  12  16  20  24  28  32  36
  5.    5  10  15  20  25  30  35  40  45
  6.    6  12  18  24  30  36  42  48  54
  7.    7  14  21  28  35  42  49  56  63
  8.    8  16  24  32  40  48  56  64  72
  9.    9  18  27  36  45  54  63  72  81
复制代码
发表于 2004-4-28 16:52:44 | 显示全部楼层

左对齐

[PHP]
#!/usr/bin/perl
for($i=1;$i<=9;$i++){
    my @a=(1..9);
    my @out=(map{$_*=$i}(@a));
format STDOUT =
@<<<@<<<@<<<@<<<@<<<@<<<@<<<@<<<@<<<
$out[0],$out[1],$out[2],$out[3],$out[4],$out[5],$out[6],$out[7],$out[8]
.
        write;
}
[/PHP]


  1. 1   2   3   4   5   6   7   8   9
  2. 2   4   6   8   10  12  14  16  18
  3. 3   6   9   12  15  18  21  24  27
  4. 4   8   12  16  20  24  28  32  36
  5. 5   10  15  20  25  30  35  40  45
  6. 6   12  18  24  30  36  42  48  54
  7. 7   14  21  28  35  42  49  56  63
  8. 8   16  24  32  40  48  56  64  72
  9. 9   18  27  36  45  54  63  72  81
复制代码
 楼主| 发表于 2004-4-28 17:32:03 | 显示全部楼层
:thank ,
format STDOUT =
@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>@>>>
$out[0],$out[1],$out[2],$out[3],$out[4],$out[5],$out[6],$out[7],$out[8]
.
    write;

这段代码什么意思,呵呵,我还没有学到呢
发表于 2004-4-28 17:55:11 | 显示全部楼层
最初由 javalee 发表
:thank ,

这段代码什么意思,呵呵,我还没有学到呢


呵呵,那就用printf来控制格式吧。

  1. for($i=1;$i<=9;$i++){
  2.     print map{sprintf("%4d",$_*$i)} 1..9;
  3.     print "\n";
  4. }
复制代码
 楼主| 发表于 2004-4-28 18:59:24 | 显示全部楼层
最初由 痴木狂澜 发表
呵呵,那就用printf来控制格式吧。

  1. for($i=1;$i<=9;$i++){
  2.     print map{sprintf("%4d",$_*$i)} 1..9;
  3.     print "\n";
  4. }
复制代码

:thank ...唉~~,该看regex了
发表于 2004-4-28 19:10:41 | 显示全部楼层
最初由 javalee 发表
:thank ,

这段代码什么意思,呵呵,我还没有学到呢

看看小骆驼书的第11章"格式"
 楼主| 发表于 2004-4-28 19:35:55 | 显示全部楼层
最初由 libinary 发表
看看小骆驼书的第11章"格式"
:
:thank,我现在的教材的是<<black book>>
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表