perl打印格式问题

时间:2010-11-23 02:38:01

标签: perl formatting

我想在perl中显示一个表,其行和列名称的长度可变。我希望列整齐排列。问题是行和列标题的长度是可变的,因此对齐会因不同的文件而移开。

以下是我用来格式化的代码:

print "\n              ";
foreach (keys(%senseToSenseCountHash))
{
 printf "%15s",$_;
}
print "\n";
print "------------------------------------------------------------\n";

my $space = "---";

foreach my $realSense (keys(%actualSenseToWronglyDisambiguatedSense))
{ 
 printf "%s",$realSense; 

        foreach (keys(%senseToSenseCountHash))
 {
   if(exists($actualSenseToWronglyDisambiguatedSense{$realSense}[0]{$_}))
   {
  printf "%15s",$actualSenseToWronglyDisambiguatedSense{$realSense}[0]{$_};
   }
   else
   {
  printf "%15s",$space;
   }
 }
 print "\n";
}

我得到的输出如下(对于我必须测试的不同文件):

                    Microsoft            IBM
------------------------------------------------------------
Microsoft            896            120
IBM             66            661



                     SERVE12         SERVE2         SERVE6        SERVE10
------------------------------------------------------------
SERVE12            319             32             19              8
SERVE2             44            159             39             25
SERVE6             22              9            102              1
SERVE10             14             16             12            494



                       HARD3          HARD2          HARD1
------------------------------------------------------------
HARD3             68              7             27
HARD2              6             60             90
HARD1             37             69            937

无论行名和列名如何,我都想使这个输出对齐。有人可以帮忙吗?

非常感谢!

2 个答案:

答案 0 :(得分:1)

这一行:

printf "%s",$realSense; 

没有特定的宽度,并且正在抛弃对齐。

答案 1 :(得分:1)

找到答案,将其粘贴在这里以防任何人想要使用它。

printf "%10s %-2s",'----------','|';

foreach(keys(%senseToSenseCountHash))
{
    printf "%s",'----------------';
}
print "\n";

printf "%10s %-2s",'          ','|';

foreach(keys(%senseToSenseCountHash))
{
    printf "%-14s",$_;
}
print "\n";

printf "%10s %-2s",'----------','|';

foreach(keys(%senseToSenseCountHash))
{
    printf "%s",'----------------';
}
print "\n";

foreach my $key (sort { $senseToSenseCountHash{$b} <=> 
                              $senseToSenseCountHash{$a} } keys %senseToSenseCountHash )
{
    $maxSense = $senseToSenseCountHash{$key};
    last;   
}

my $space = "---";

foreach my $realSense (keys(%actualSenseToWronglyDisambiguatedSense))
{

    printf "%-10s %-2s",$realSense,'|'; 
        foreach (keys(%senseToSenseCountHash))
    {
        if(exists($actualSenseToWronglyDisambiguatedSense{$realSense}[0]{$_}))
        {
            printf "%-15s",$actualSenseToWronglyDisambiguatedSense{$realSense}[0]{$_};
        }
        else
        {
            printf "%-15s",$space;
        }
    }
    print "\n";
}

printf "%10s %-2s",'----------','|';

foreach(keys(%senseToSenseCountHash))
{
    printf "%s",'----------------';
}
print "\n";

输出:

---------- | ------------------------------------------------
           | HARD3         HARD2         HARD1         
---------- | ------------------------------------------------
HARD3      | 68             7              27             
HARD2      | 6              60             90             
HARD1      | 37             69             937            
---------- | ------------------------------------------------


---------- | ----------------------------------------------------------------
           | SERVE12       SERVE2        SERVE6        SERVE10       
---------- | ----------------------------------------------------------------
SERVE12    | 319            32             19             8              
SERVE2     | 44             159            39             25             
SERVE6     | 22             9              102            1              
SERVE10    | 14             16             12             494            
---------- | ----------------------------------------------------------------