why would it be a bad idea for them to be merged into the same data structure?

Sometimes it is a good idea to merge them. For these occasions there are modules that provide the mapping ability of hashes and the ordering ability of arrays in the same data object.

Often, hash lookup is needed but ordering is not required. In these cases, it would be a waste of resource to maintain un-needed ordering of the hash keys. For small data sets the difference wouldn't be very much but Perl can handle very large hashes. For these large hashes, the work of inserting a new element would be significantly greater if the new key had to be inserted into an ordered list.

Similarly, if all that is needed is an ordered list of items, the implementation of the numerically indexed array is more efficient than that of the hash lookup. One could use a hash with keys 0, 1, 2, etc. but it would be slower than an array for many operations.

So, the reason it would be bad to merge arrays and hashes is that doing so would make many common cases less efficient (slower and/or consuming more resource).


In reply to Re: Difference between array and a hash? (Newbie concerns) by ig
in thread Difference between array and a hash? (Newbie concerns) by reztlaf

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.