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开发者_C百科.
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).
It's amazing how people complicate easy stuff!
You don't need the @fields array. Please read the perldoc about keys, values and sort.
# untested sketch
my %description = ( prop23 => "foo", prop24 => "bar" );
foreach my $key(sort (keys %description)) {
    print $key, " is: ", $description{$key}, "\n";   # or whatever
}
Addendum: Regarding the order of keys, just do the following:
my @arbitraryOrder = qw(prop42 prop35 prop1 ...);  # allows to map number to key
my %keytoNumber = ();                              # will map keys to numbers
foreach my $i(0..$#arbitraryOrder) $keyToNumber{$arbitraryOrder[$i]} = $i;
Writing the comparison function for sort is left as an exercise :)
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:
use warnings;
use strict;
use Tie::IxHash; 
tie my %H, "Tie::IxHash"; 
$H{foo} = 1; 
$H{bar} = 2; 
# order of keys is now always 'foo', 'bar'
print keys %H;
EDIT2: I've tried it out, and it actually works:
#!/usr/bin/perl -w
use strict;
use Template;
use Tie::IxHash;
# my %h; # this breaks ordering
tie my %H, "Tie::IxHash"; # this keeps ordering
@H{qw/f oo b a r/} = 1..100;
# don't define $H{'keys'} or you'll get disappointed
my $tpl = Template->new();
$tpl->process(\*DATA, {hash=>\%H});
__DATA__
[% FOREACH k IN hash.keys %]
     [% k %] => [% hash.$k %]
[% END %]
If you need ordering and multiple pieces of information then you should consider an array of hash references.
my @fields = (
  { id => 'property_foo',
    label => 'Some property' },
  { id => 'property_bar',
    label => 'Important field' },
  { id => 'property_baz',
    label => 'Something else' },
);
foreach (@fields) {
  print "ID: $_->{id}, Label: $_->{label}\n";
}
If the complexity increases much beyond this, you might consider replacing the hashrefs with real objects.
And, in TT, it looks like this:
[%-
properties = [
  {id => 'property_foo',
   label => 'Some property'},
  {id => 'property_bar',
   label => 'Important field'},
  {id => 'property_baz',
   label => 'Something else'},
];
-%]
[%- FOREACH property IN properties %]
    [% property.label %]: <input name="[% property.id %]" />
[% END %]
Actually if you want your hash alphabetically sorted by key, Template::Toolkit does that for you.
test.pl
use strict;
use warnings;
use Template;
my %hash = qw' a 1 b 2 c 3 ';
my $config = {
  INCLUDE_PATH => '/search/path',
};
my $input = 'test.tt2';
my $template = Template->new( $config );
$template->process( $input, {
  hash => \%hash,
})
test.tt2
[% FOREACH hash -%] [% key %] => [% value %] [% END %]
output
a => 1 b => 2 c => 3
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论