arrays - Perl 将哈希值数组输出为表

标签 arrays perl hash

如果你有一个哈希数组,其中键代表列名,值是行内容,在 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/

相关文章:

arrays - "Value of type ' AnyObject ' has no member."..尝试在 Swift 中为数组中的对象赋值时发生错误

ruby - ruby 如何处理数组范围访问?

ruby - 哈希方法使用什么相等性?

php - MySQL 哈希函数实现

ruby - 如何从数组构建散列

Javascript:切片和附加 keyValue 后重复数组不正确

c - 用空格迭代字符串

javascript - eval_in_page javascript 执行在 firefox 中有效,但在 phantomjs 中无效

perl - 如何在 Perl 中替换任意固定字符串

python - 在 Python 中的 re.sub 中获取组匹配