perl打印格式问题

发布于 2024-10-04 01:47:59 字数 1663 浏览 3 评论 0原文

我想在 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

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

非常感谢!

I want to display a table in perl, the rows and column names for which will be of variable length. I want the columns to be neatly aligned. The problem is the row and column heading are of variable length, so the alignment shifts off for different files.

Here is the code I am using to format :

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";
}

The outputs I get are as follows (for different files that I have to test on) :

                    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

I want to make this output aligned regardless of the row and column name. Can anyone please help?

Thanks so much!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你没皮卡萌 2024-10-11 01:47:59

这条线:

printf "%s",$realSense; 

没有特定的宽度,并且偏离了对齐方式。

This line:

printf "%s",$realSense; 

has no specific width, and is throwing off the alignment.

世俗缘 2024-10-11 01:47:59

找到答案,将其粘贴到此处,以防有人想要使用它。

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            
---------- | ----------------------------------------------------------------

Found the answer, pasting it here in case any one wants to use it.

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";

Output :

---------- | ------------------------------------------------
           | 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            
---------- | ----------------------------------------------------------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文