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)
In reply to Perl hashes and non-determinism by Ratazong
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |