On 7/14/06, Alan Winston - SSRL Central Computing
<[EMAIL PROTECTED]> wrote:
Somebody who knows something should respond to this, really, but a WAG at how
to do this, if your hash is static once set up:
my $ordinal, $index, %ordinalhash, %indexhash;
$ordinal = 0;
foreach $index (sort keys %yourhash) {
$ordinal++;
$ordinalhash{$ordinal} = $index;
$indexhash($index) = $ordinal;
}
To get your "what is the next index after this index", you can do
$next_index = $ordinal_hash{$indexhash{$this_index}+1};
and for the previous index
$previous_index = $ordinal_hash{$indexhash{$this_index}-1};
There's already a couple modules on CPAN for this, Tie::Hash::Sorted
and Tie::SortHash do internally. They likely do some variation of
your algorithm.
However, as they are tied, hash performance will be a smoking ruin.