Imagine the case when an array holding key/value pairs is assigned to an hash (i.e. normal hash initialization). What exactly happens when the key gets repeated, i.e. is twice or more times inside the array (on odd positions of course)? Of course this is like assigning this hash key twice, e.g. the second assignment simple overwrites the first.
my @array = ( 'A', 1, 'B', 2, 'C', 3, 'A', 4 ); my %hash = @array; # $hash{A} == 4 or 1 or depends??
Now my actual question: Is this a defined, stable thing, e.g. always the last key in the array would win? Or is it - for any reason - the first? Is it not explicitly defined and depends on something like the internal hash structure, e.g. the exact key names?

I came to this question when I had code where I have to add certain key/value pairs to an existing hash and want to make sure that already existing keys are overwritten:

%hash = (%hash,@array); # or %hash = (%hash,%otherhash);
of course I could just take a loop for this but I like the simplicity of this line and because the existing hash isn't very big the reassigning to itself is not very expensive. Now I would like to know if this really works like I think it does.

In reply to Assigning list with duplicate keys to hash by mscharrer

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.