模板工具包中的 Perl 多维哈希
Perl Multidimensional Hash in Template Toolkit
我有以下 perl 多维哈希作为哈希引用从 Dancer 传递到 Template Toolkit。我无法弄清楚如何在 Template Toolkit 中显示它。
$VAR1 = {
'TylerMontgomery(2022)' => {
'so' => 1,
'bb' => 1,
'rbis' => 0,
'atbats' => 7,
'runs' => 2,
'hits' => 2
},
'ChaseLangan(2022)' => {
'runs' => 4,
'hits' => 4,
'atbats' => 5,
'bb' => 0,
'rbis' => 2,
'so' => 1
},
'BryceJones(2021)' => {
'hits' => 2,
'runs' => 2,
'atbats' => 4,
'bb' => 1,
'rbis' => 4,
'so' => 1
},
'WillGrimes(2021)' => {
'bb' => 0,
'rbis' => 0,
'so' => 1,
'runs' => 1,
'hits' => 2,
'atbats' => 3
},
};
我可以使用以下代码在我的 perl 代码中插入散列:
foreach my $name (sort keys %season) {
printf "%-27.27s", "$name: ";
foreach my $stat (sort keys %{ $season{$name} }) {
printf "%-12.12s", "$stat: $season{$name}{$stat} ";
## cal. avg
$season{$name}{AVG} = $season{$name}{hits} / $season{$name}{atbats};
}
printf "%4s %.3f\n", "avg:", $season{$name}{AVG};
}
到目前为止,我为显示它所做的尝试似乎有些不合时宜。任何帮助将不胜感激。
<table style="width:100%; line-height:40px;">
<% FOREACH Season = Season %>
<tr>
<td width="5">Season.key <% Season.key %></td>
<td width="5">Season.val <% Season.value %></td>
<td width="5">Season.val.atbats <% Season.value.atbats %>
<td width="5">Season.val.hits <% Season.value.hits %>
</tr>
<% END %>
</table>
最终在网页上显示的内容:
HASH(0xabd1ef4)HASH(0xabd1ef4)
Season.key Season.val Season.val.atbats Season.val.hits
FOREACH
遍历数组。遍历 hash.keys
.
返回的数组
<% FOREACH id IN payload.keys %>
<% season = payload.$id %>
<p><% season.runs %></p>
<p><% season.atbats %></p>
<% END %>
完整代码:
use Template qw( );
my %seasons = (
'TylerMontgomery(2022)' => {
'so' => 1,
'bb' => 1,
'rbis' => 0,
'atbats' => 117,
'runs' => 2,
'hits' => 2
},
'ChaseLangan(2022)' => {
'runs' => 4,
'hits' => 24,
'atbats' => 5,
'bb' => 0,
'rbis' => 2,
'so' => 1
},
'BryceJones(2021)' => {
'hits' => 2,
'runs' => 2,
'atbats' => 4,
'bb' => 2,
'rbis' => 4,
'so' => 1
},
);
my $tt = Template->new({
START_TAG => '<%',
END_TAG => '%>',
});
my $template = <<'__EOS__';
<!DOCTYPE html>
<html lang="en">
<head>
<!-- head definitions go here -->
<meta charset="utf-8">
</head>
<body>
<div class="container" style="margin-top:20px;">
<% FOREACH id IN seasons.keys %>
<% season = seasons.$id %>
<p><% season.runs %></p>
<p><% season.atbats %></p>
<% END %>
</div>
</body>
</html>
__EOS__
$tt->process($template, {
title => 'Get Softball Season Stats',
seasons => \%seasons,
})
or die($tt->error);
输出
<!DOCTYPE html>
<html lang="en">
<head>
<!-- head definitions go here -->
<meta charset="utf-8">
</head>
<body>
<div class="container" style="margin-top:20px;">
<p>4</p>
<p>5</p>
<p>2</p>
<p>117</p>
<p>2</p>
<p>4</p>
</div>
</body>
</html>
注意:不要对合集和单季使用 season
。我使用了更好的变量名称,你应该调整你的数据以匹配。
我有以下 perl 多维哈希作为哈希引用从 Dancer 传递到 Template Toolkit。我无法弄清楚如何在 Template Toolkit 中显示它。
$VAR1 = {
'TylerMontgomery(2022)' => {
'so' => 1,
'bb' => 1,
'rbis' => 0,
'atbats' => 7,
'runs' => 2,
'hits' => 2
},
'ChaseLangan(2022)' => {
'runs' => 4,
'hits' => 4,
'atbats' => 5,
'bb' => 0,
'rbis' => 2,
'so' => 1
},
'BryceJones(2021)' => {
'hits' => 2,
'runs' => 2,
'atbats' => 4,
'bb' => 1,
'rbis' => 4,
'so' => 1
},
'WillGrimes(2021)' => {
'bb' => 0,
'rbis' => 0,
'so' => 1,
'runs' => 1,
'hits' => 2,
'atbats' => 3
},
};
我可以使用以下代码在我的 perl 代码中插入散列:
foreach my $name (sort keys %season) {
printf "%-27.27s", "$name: ";
foreach my $stat (sort keys %{ $season{$name} }) {
printf "%-12.12s", "$stat: $season{$name}{$stat} ";
## cal. avg
$season{$name}{AVG} = $season{$name}{hits} / $season{$name}{atbats};
}
printf "%4s %.3f\n", "avg:", $season{$name}{AVG};
}
到目前为止,我为显示它所做的尝试似乎有些不合时宜。任何帮助将不胜感激。
<table style="width:100%; line-height:40px;">
<% FOREACH Season = Season %>
<tr>
<td width="5">Season.key <% Season.key %></td>
<td width="5">Season.val <% Season.value %></td>
<td width="5">Season.val.atbats <% Season.value.atbats %>
<td width="5">Season.val.hits <% Season.value.hits %>
</tr>
<% END %>
</table>
最终在网页上显示的内容:
HASH(0xabd1ef4)HASH(0xabd1ef4)
Season.key Season.val Season.val.atbats Season.val.hits
FOREACH
遍历数组。遍历 hash.keys
.
<% FOREACH id IN payload.keys %>
<% season = payload.$id %>
<p><% season.runs %></p>
<p><% season.atbats %></p>
<% END %>
完整代码:
use Template qw( );
my %seasons = (
'TylerMontgomery(2022)' => {
'so' => 1,
'bb' => 1,
'rbis' => 0,
'atbats' => 117,
'runs' => 2,
'hits' => 2
},
'ChaseLangan(2022)' => {
'runs' => 4,
'hits' => 24,
'atbats' => 5,
'bb' => 0,
'rbis' => 2,
'so' => 1
},
'BryceJones(2021)' => {
'hits' => 2,
'runs' => 2,
'atbats' => 4,
'bb' => 2,
'rbis' => 4,
'so' => 1
},
);
my $tt = Template->new({
START_TAG => '<%',
END_TAG => '%>',
});
my $template = <<'__EOS__';
<!DOCTYPE html>
<html lang="en">
<head>
<!-- head definitions go here -->
<meta charset="utf-8">
</head>
<body>
<div class="container" style="margin-top:20px;">
<% FOREACH id IN seasons.keys %>
<% season = seasons.$id %>
<p><% season.runs %></p>
<p><% season.atbats %></p>
<% END %>
</div>
</body>
</html>
__EOS__
$tt->process($template, {
title => 'Get Softball Season Stats',
seasons => \%seasons,
})
or die($tt->error);
输出
<!DOCTYPE html>
<html lang="en">
<head>
<!-- head definitions go here -->
<meta charset="utf-8">
</head>
<body>
<div class="container" style="margin-top:20px;">
<p>4</p>
<p>5</p>
<p>2</p>
<p>117</p>
<p>2</p>
<p>4</p>
</div>
</body>
</html>
注意:不要对合集和单季使用 season
。我使用了更好的变量名称,你应该调整你的数据以匹配。