In some code I reviewed for a colleague was a hash defined something like the following (a simplified version) :

my %fred = ( [1, 2, 3] => [1, 1, 0], [3, 4, 5] => [0, 1, 0], [0, 2, 4] => [1, 2, 1], ::: );

The code accesses this using the array references as a key, and as the "key" arrays are all distinct these references must be unique. So no problem there.

But the code later uses another hash constructed by calling reverse() on the above, and it seems to me that may be problematic if any of the anonymous "value" arrays are equal, which in the circs it appears they may well be, because perl may be clever enough to spot identical value arrays and use one copy for any such subsets, in which case reverse() would lose all duplicate entries at random.

Even if perl does not do that today, who is to say it won't start being done in some future version?

My colleague who wrote the code assures me this problem won't arise because (in his words) "the hash values are references to arrays of values, not the array values themselves". But I'm not convinced - If more than one identical value array shares the same location, then of course their references will be the same.

Any ideas?

Regards

John R Ramsden


In reply to Allocation of anonymous arrays by OwlHoot

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.