On a user level, not behind the scenes, a hash and an array are exactly the same thing except one takes arbitrary strings/scalars as their keys and arrays uses only non negative numbers. There are nuances to how they are used in perl, but in a general sense this is true.

If you can easily map everything to the non-negative numbers integers from 0 through some length/count of the items you have, an array is great. Examples are an already sorted list of names. Want to know who's 5th, just do $names[5]. Arrays hold order very well, since they map to non-negative numbers and are kinda designed as a continuous data structure.

If you need arbitrary access to information where order or mapping isn't easy or makes sense, a hash is great. For instance, if I put my name, address and other personal info in a single structure, a hash rocks. $person{first_name} would give me a person's first_name.

To give the counter examples of when they suck, keeping a sorted list of people in a hash would be a little odd. I can do $person{5}, but what would $person{beer} mean in the context? Should it be allowed? It's just, strange.

Using an array for data that has no easy mapping to say, a function that is natural, an array kinda sucks. For instance, $person[0] being the first name, $person[1] being the last isn't as natural as $person{first_name}.

The rest is just engineering. They are both fairly fast, it's a matter of seeing which tool fits the job better.

Update: Wtf.. I used number when I meant integer. I'm an idiot. What i meant and typed were inconsistent. Thanks brian_d_foy.

----
Give me strength for today.. I will not talk it away..
Just for a moment.. It will burn through the clouds.. and shine down on me.


In reply to Re: Use Hash? Array? Still confused after all these years. by exussum0
in thread Use Hash? Array? Still confused after all these years. by hmbscully

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.