Hello Monks,
I have defined a hash and assigned values to it as follows:

my(%sortListArr); $sortListArr{'1'} = 'A1'; $sortListArr{'2'} = 'A2'; $sortListArr{'3'} = 'A3'; $sortListArr{'4'} = 'A4'; $sortListArr{'5'} = 'A5'; $sortListArr{'6'} = 'A6';

Then when I do a:

foreach $key (keys %sortListArr){ print $sortListArr{$key},"\n"; }

The o/p is A1, A2....A6. However upon adding a 7th value:

$sortListArr{'7'} = 'A7';

The o/p becomes A7, A1, A2, A3...A6. Any explanations to this mystery? The problem disappears if I assign the values as:

$sortListArr{'0'} = 'A1'; $sortListArr{'1'} = 'A2'; $sortListArr{'2'} = 'A3'; $sortListArr{'3'} = 'A4'; $sortListArr{'4'} = 'A5'; $sortListArr{'5'} = 'A6'; $sortListArr{'6'} = 'A7';

The o/p is then A1, A2.....A7.

Tamojit

20050407 Janitored by Corion: Added formatting


In reply to Mysterious hash behaviour by Anonymous Monk

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.