title: Array of sorted key/value pairs to sorted array of keys and hash
body: <p>I am developing a Catalyst app which uses <code>Template::Toolkit</code> 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.</p>
  
  <p>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 <strong>in TT</strong>? I could use both things but that seems ugly and can cause mistakes when changing the fields.</p>
  
  <p>However, I prefer doing this in TT because both the descriptions and the order of form elements is a front-end thing.</p>
  
  <p>This is how I would do it in pure Perl:</p>
  
  <pre><code>#!/usr/bin/perl -w
  
  use 5.10.0;
  
  # definition of description and order in 1 step
  my @fields = (
          property_foo =&gt; "Some property",
          property_bar =&gt; "Important field",
          property_baz =&gt; "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 $_]";
  }
  </code></pre>
  
  <p>Outputs:</p>
  
  <pre><code>=== 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]
  </code></pre>
  
  <p>This is what I write in my template:</p>
  
  <pre><code>[%
  order = (
      property_foo,
      property_bar,
      property_baz,
  );
  
  descriptions = {
      property_foo =&gt; "Some property",
      property_bar =&gt; "Important field",
      property_baz =&gt; "Something else",
  }
  
  FOREACH property IN order %]
      [% descriptions.$property %]: &lt;input name="[% property %]" /&gt;
  [% END %]
  </code></pre>
  
  <p>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).</p>