Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl在不使用对象的情况下创建格式化表_Perl - Fatal编程技术网

Perl在不使用对象的情况下创建格式化表

Perl在不使用对象的情况下创建格式化表,perl,Perl,我只想在表中输出一些结果,而不存在任何偏移问题。为了清楚起见,不要担心foreach和值的输出只是伪代码 print "\n ______________________________________________________"; print "\n | |"; print "\n | Title

我只想在表中输出一些结果,而不存在任何偏移问题。为了清楚起见,不要担心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
语句来写入该格式。您也可以指定表格的页眉和页脚。

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

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

您所在的城市/规模是多少?阵列?哈希?我喜欢使用Text::FormatTable模块来处理这类事情。一个好的起点: