如果你有一个哈希数组,其中键代表列名,值是行内容,在 Perl 中将其输出为表格的最佳方法是什么?
最佳答案
每行是否具有相同的哈希键?这就是您将得到的结构,例如来自 DBI,对应于通常理解的表属性(即每行具有相同的列集)。以下是我的想法示例,希望它符合您的想法:
my @AoH = (
{id => 1, name => 'Dick'},
{id => 2, name => 'Jane'},
);
在这种情况下,您通常知道这些列是什么。我将做出这个假设。代码如下:
my @cols = qw(id name);
my @AoH; # as above
# print the column headings
print join "\t", @cols;
# print values for each row using a hash slice
for my $row_ref (@AoH) {
print join "\t", @$row_ref{@cols};
}
关于arrays - Perl 将哈希值数组输出为表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4819820/