The concept of an ordered hash is incorrect. Although the array is an ordered list of elements and therefore accessible by index, the hash is a (non-ordered) set of key-value pairs, where the keys are sets.
To complete your task, you will have to sort the keys by the order property:
my @sorted = sort {$hash{$a}{order} <=> $hash{$b}{order}} keys %$itemHash;
Then you can create key-value pairs via map :
my @sortedpairs = map {$_ => $itemHash->{$_}} @sorted;
We could wrap this in a sub:
sub ridiculousEach { my %hash = @_; return map {$_ => $hash{$_}} sort {$hash{$a}{order} <=> $hash{$b}{order}} keys %hash; }
to get a list of elements of key value with even size
sub giveIterator { my %hash = @_; my @sorted = sort {$hash{$a}{order} <=> $hash{$b}{order}} keys %hash; return sub { my $key = shift @sorted; return ($key => $hash{$key}); }; }
to create a callback that is drop down for each.
Then we can:
my $iterator = giveIterator(%$itemHash); while (my ($tag, $item) = $iterator->()) { ...; }
There is a serious flaw in this approach: each uses only two elements at a time and therefore works in read-only memory. This solution should read the entire hash and store an array of all keys. Minor with small hashes, this can become important with a very large number of elements.
amon source share