Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The concept of an "ordered hash" is wrong. While an array is an ordered list of elements, and therefore accessible by index, a hash is an (un-ordered) collection of key-value pairs, where the keys are a set.</p> <p>To accomplish your task, you would have to sort the keys by the <code>order</code> property:</p> <pre><code>my @sorted = sort {$hash{$a}{order} &lt;=&gt; $hash{$b}{order}} keys %$itemHash; </code></pre> <p>You can then create the key-value pairs via <code>map</code>:</p> <pre><code>my @sortedpairs = map {$_ =&gt; $itemHash-&gt;{$_}} @sorted; </code></pre> <p>We could wrap this up into a sub:</p> <pre><code>sub ridiculousEach { my %hash = @_; return map {$_ =&gt; $hash{$_}} sort {$hash{$a}{order} &lt;=&gt; $hash{$b}{order}} keys %hash; } </code></pre> <p>to get an even-sized list of key-value elements, and</p> <pre><code>sub giveIterator { my %hash = @_; my @sorted = sort {$hash{$a}{order} &lt;=&gt; $hash{$b}{order}} keys %hash; return sub { my $key = shift @sorted; return ($key =&gt; $hash{$key}); }; } </code></pre> <p>to create a callback that is a drop-in for the each.</p> <p>We can then do:</p> <pre><code>my $iterator = giveIterator(%$itemHash); while (my ($tag, $item) = $iterator-&gt;()) { ...; } </code></pre> <p>There is a severe drawback to this approach: <code>each</code> only uses two elements at a time, and thus operates in constant memory. This solution has to read in the whole hash and store an array of all keys. Unnoticable with small hashes, this can get important with a very large amount of elements.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload