0

我想简单地在表格中输出一些结果,而不会出现任何偏移问题。为了清楚起见,不要担心只是伪代码的 foreach 和值的输出。

print "\n  ______________________________________________________";
print "\n |                                                      |";
print "\n |                        Title                         |";
print "\n +______________________________________________________+";
print "\n |                          |                           |";
print "\n |          City            |          Size             |";
print "\n |__________________________|___________________________|";

#Sort by highest scores
################################
foreach (city, size)
{
print "\n | (city(value)";
print "| (size(value)";
}

有任何想法吗?

4

2 回答 2

5

它已经很少使用了,但是 Perl 有内置的能力来创建这些类型的表单

基本上,您使用规范来说明您希望这些表格的格式,以及这些表格中的信息将使用该format语句放置的位置。然后,您使用 Perlwrite语句写入该格式。您也可以指定表格的页眉和页脚。

于 2012-12-06T14:40:14.347 回答
2

我建议您使用 substr 覆盖模板行的正确部分。

use strict;
use warnings;

my %data = (
  Birmingham  => 1_000_000,
  Bristol     => 430_000,
  Manchester   => 110_000,
);

print "  ______________________________________________________\n";
print " |                                                      |\n";
print " |                        Title                         |\n";
print " +______________________________________________________+\n";

my $template =
      " |                          |                           |\n";
print $template;

while (my ($city, $size) = each %data) {
  my $line = $template;
  substr $line, 12, length $city, $city;
  substr $line, 39, length $size, $size;
  print $line;
}

print " |__________________________|___________________________|\n";

输出

  ______________________________________________________
 |                                                      |
 |                        Title                         |
 +______________________________________________________+
 |                          |                           |
 |          Bristol         |          430000           |
 |          Manchester      |          110000           |
 |          Birmingham      |          1000000          |
 |__________________________|___________________________|
于 2012-12-06T14:55:40.923 回答