Ratazong has asked for the wisdom of the Perl Monks concerning the following question:
Dear monks,
I am participating in a coding-contest, where you have to steer a bot through an arena and collect gems. (hidden gems (german only)). I am one of only two contestants using perl.
As part of the rules the bot must behave deterministic - two runs in the same arena need to result in the same result. For random numbers this is ensured by setting a fixed seed in srand().
The challenge comes with hashes, which by design are unordered. Here I ensure determinism using sort on the keys. But now I want to access the hash based on the order of a field inside:
- and the values are not unique. This line resulted in non-determinism (and therefore my bot was excluded from tonights competition).foreach my $k sort { $hash{$a}{value} <=> $hash{$b}{value} } (keys %ha +sh)
Is there a way to force perl to access a hash in a deterministic way? Similar to srand()? Or what is the preferred way to enforce it? I could try something like
but that doesn't seem to be elegant to me ... Rata (waiting for enlightment)foreach my $k sort { $hash{$a}{value} <=> $hash{$b}{value} } (sort key +s %hash)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl hashes and non-determinism
by hippo (Archbishop) on Nov 19, 2025 at 10:11 UTC | |
|
Re: Perl hashes and non-determinism
by ikegami (Patriarch) on Nov 19, 2025 at 18:53 UTC | |
by Ratazong (Monsignor) on Nov 21, 2025 at 11:41 UTC | |
|
Re: Perl hashes and non-determinism
by ikegami (Patriarch) on Nov 19, 2025 at 19:07 UTC | |
by NERDVANA (Priest) on Nov 21, 2025 at 04:24 UTC | |
|
Re: Perl hashes and non-determinism
by sleet (Pilgrim) on Nov 19, 2025 at 19:00 UTC | |
|
Re: Perl hashes and non-determinism
by ikegami (Patriarch) on Nov 19, 2025 at 19:15 UTC | |
|
Re: Perl hashes and non-determinism
by cavac (Prior) on Nov 19, 2025 at 09:12 UTC |