我想简单地在表中输出一些结果,而没有任何偏移问题。为了清楚起见,不要担心 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)";
}
有什么想法吗?
它很少被使用,但 Perl 具有创建这些类型表单的内置能力。
基本上,您使用规范来说明您希望如何设置这些表的格式,以及使用 format
语句将这些表中的信息放置在哪里。然后,使用 Perl write
语句写入该格式。您也可以指定表的页眉和页脚。
我建议你使用 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 |
|__________________________|___________________________|