Hiya,

This is probably quite simple but I can't figure out why it does it...

I basically want to print the key-value pairs in the order they are written, but for some reason this does not happen.

%permissions = ('ATM_NO' => 'No access to ATMs', 'ATM_R' => 'Read ATMs', 'ATM_W' => 'Write ATMs', 'ATM_M' => 'Modify ATMs', 'ATM_D' => 'Delete ATMs', 'ETM_NO' => 'No access to ETMs', 'ETM_R' => 'Read ETMs', 'ETM_W' => 'Write ETMs', 'ETM_M' => 'Modify ETMs', 'ETM_D' => 'Delete ETMs' ); while (($perm_name, $perm_desc) = each(%permissions)) { print "\nINSERT INTO permissions SET name='$perm_name', descriptio +n='$perm_desc'\n"; }
prints out...
INSERT INTO permissions SET name='ATM_D', description='Delete ATMs' INSERT INTO permissions SET name='ATM_W', description='Write ATMs' INSERT INTO permissions SET name='ATM_M', description='Modify ATMs' INSERT INTO permissions SET name='ETM_W', description='Write ETMs' INSERT INTO permissions SET name='ETM_D', description='Delete ETMs' INSERT INTO permissions SET name='ATM_NO', description='No access to A +TMs' INSERT INTO permissions SET name='ATM_R', description='Read ATMs' INSERT INTO permissions SET name='ETM_NO', description='No access to E +TMs' INSERT INTO permissions SET name='ETM_M', description='Modify ETMs' INSERT INTO permissions SET name='ETM_R', description='Read ETMs'
Why is it in that order? It doesnt appear to be related to the values or alphabetical in any way, it also seems to be consistent across other hashes as well. Sorting them will not work as an order different to the one written will be generated, does anyone have any ideas?

Thanks for your time, dalton


In reply to Print a hash in order 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.