排序的键/值对数组到排序的键和哈希数组
我正在开发一个 Catalyst 应用程序,它使用 Template::Toolkit
作为模板引擎。一个页面需要一系列相同的输入元素。它们可以从数组中获取,但我需要元素的排序顺序和描述性标签。
为了获得排序顺序,我将使用数组。为了存储每个键的附加值,散列是完美的。如何在 TT 中将两者结合起来?我可以使用这两种东西,但这看起来很难看,并且在更改字段时可能会导致错误。
然而,我更喜欢在 TT 中这样做,因为表单元素的描述和顺序都是前端的事情。
这就是我在纯 Perl 中的做法:
#!/usr/bin/perl -w
use 5.10.0;
# definition of description and order in 1 step
my @fields = (
property_foo => "Some property",
property_bar => "Important field",
property_baz => "Something else",
);
# extract information
my %descriptions = @fields;
my @order = @fields[grep {($_ + 1) % 2} 0..(scalar @fields - 1)];
say "=== natural perl sort order ===";
foreach (keys %descriptions) {say $_};
say "=== wanted output ===";
foreach (@order) {
say $descriptions{$_} . ": [label for $_]";
}
输出:
=== natural perl sort order ===
property_baz
property_foo
property_bar
=== wanted output ===
Some property: [label for property_foo]
Important field: [label for property_bar]
Something else: [label for property_baz]
这是我在模板中编写的内容:
[%
order = (
property_foo,
property_bar,
property_baz,
);
descriptions = {
property_foo => "Some property",
property_bar => "Important field",
property_baz => "Something else",
}
FOREACH property IN order %]
[% descriptions.$property %]: <input name="[% property %]" />
[% END %]
但是,两次拥有相同的信息(字段列表)确实很难看。我想避免两次编辑列表,并且字段列表较长,这会变得非常烦人(大约 20 个项目,不够长,无法执行一些数据库操作)。
I am developing a Catalyst app which uses Template::Toolkit
as template engine. One page needs a list of equal input elements. They can be taken from an array but I need both sort order and a descriptive label for the element.
For having a sort order I would use an array. For storing an additional value per key a hash is perfect. How to combine both in TT? I could use both things but that seems ugly and can cause mistakes when changing the fields.
However, I prefer doing this in TT because both the descriptions and the order of form elements is a front-end thing.
This is how I would do it in pure Perl:
#!/usr/bin/perl -w
use 5.10.0;
# definition of description and order in 1 step
my @fields = (
property_foo => "Some property",
property_bar => "Important field",
property_baz => "Something else",
);
# extract information
my %descriptions = @fields;
my @order = @fields[grep {($_ + 1) % 2} 0..(scalar @fields - 1)];
say "=== natural perl sort order ===";
foreach (keys %descriptions) {say $_};
say "=== wanted output ===";
foreach (@order) {
say $descriptions{$_} . ": [label for $_]";
}
Outputs:
=== natural perl sort order ===
property_baz
property_foo
property_bar
=== wanted output ===
Some property: [label for property_foo]
Important field: [label for property_bar]
Something else: [label for property_baz]
This is what I write in my template:
[%
order = (
property_foo,
property_bar,
property_baz,
);
descriptions = {
property_foo => "Some property",
property_bar => "Important field",
property_baz => "Something else",
}
FOREACH property IN order %]
[% descriptions.$property %]: <input name="[% property %]" />
[% END %]
However, it is really ugly to have the same information (list of fields) twice. I want to avoid editing the list twice and with a longer list of fields it gets really annoying (about 20 items, not long enough to do some database stuff).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
令人惊奇的是人们如何将简单的事情复杂化!
您不需要 @fields 数组。请阅读有关键、值和排序的perldoc。
附录:关于键的顺序,只需执行以下操作:
编写排序的比较函数留作练习:)
It's amazing how people complicate easy stuff!
You don't need the @fields array. Please read the perldoc about keys, values and sort.
Addendum: Regarding the order of keys, just do the following:
Writing the comparison function for sort is left as an exercise :)
您可能对 Tie::IxHash
它是一个“散列”,保持添加键的顺序(值更新不影响排序)。
编辑:一个简短的例子:
编辑2:我已经尝试过,它实际上有效:
You might be interested in Tie::IxHash
It is a "hash" that keeps the order in which you add keys (value updates do not affect sorting).
EDIT: A brief example:
EDIT2: I've tried it out, and it actually works:
如果您需要排序和多条信息,那么您应该考虑哈希引用数组。
如果复杂性增加远远超出此范围,您可能会考虑用真实对象替换哈希引用。
在 TT 中,它看起来像这样:
If you need ordering and multiple pieces of information then you should consider an array of hash references.
If the complexity increases much beyond this, you might consider replacing the hashrefs with real objects.
And, in TT, it looks like this:
实际上,如果您希望哈希按键按字母顺序排序,Template::Toolkit 可以为您做到这一点。
test.pl
test.tt2
输出
Actually if you want your hash alphabetically sorted by key, Template::Toolkit does that for you.
test.pl
test.tt2
output