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:

foreach my $k sort { $hash{$a}{value} <=> $hash{$b}{value} } (keys %ha +sh)
- and the values are not unique. This line resulted in non-determinism (and therefore my bot was excluded from tonights competition).

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

foreach my $k sort { $hash{$a}{value} <=> $hash{$b}{value} } (sort key +s %hash)
but that doesn't seem to be elegant to me ...

Rata (waiting for enlightment)

In reply to Perl hashes and non-determinism by Ratazong

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.