Fellow monks, this weeks feels like cyocum stupid question week but I cannot think of a more elegant way of doing this so I am asking your advice.

I am writing a chess program as some of you may remember (the code in there right now is very sub-optimal and I am thinking about trashing it and starting over so beware if you check out the code). One of the big probelms that is slowing it down is move generation so I determined to preprocess all valid moves for all peices and store them so that the piece objects can share the data. The preprocessing is now finished and I have moved on to the next step, which is representing this the best way in Perl code.

I figure that a hash of hashes with the value being an anonymous array of arrays with all the valid moves contained in them. Now, my problem is how to best represent this.

if I do this:

use strict; use warnings; #row #col #list of list of valid moves our %hash = (0 => 0 => [[[1, 0],[2, 0],[3, 0],[4, 0],[5, 0],[6, 0],[7, + 0]], [[0, 1],[0, 2],[0, 3],[0, 4],[0, 5],[0, 6],[0, 7]]]);

I get this:

Odd number of elements in hash assignment

This forces me to do this:

use strict; use warnings; our %hash; $hash{0}->{0} = [[[1, 0],[2, 0],[3, 0],[4, 0],[5, 0],[6, 0],[7, 0]], [[0, 1],[0, 2],[0, 3],[0, 4],[0, 5],[0, 6],[0, 7]]];

This works but is ugly and does not really give the reader an indication that this is a shared piece of data that never changes. If I were doing this in C/C++, I would make it all const.

Any ideas or examples of how to make this look better would be highly appreciated. As always, thank you all for your time and effort.


In reply to Representing Complex but Static Data in Perl by cyocum

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.