Arrays and hashes are both bunches of pigeon holes where you can store values. The difference is how you get at any particular pigeon hole.

For an array each pigeon hole has a number (the 'index') which ranges from 0 to one less than the number of pigeon holes.

For a hash each pigeon hole has a label and you access the pigeon hole by whatever is written on the label. I understand that PHP only has labeled pigeon holes (hashes), but of course if you label them with a sequence of numbers from 0 to n - 1 the effect is somewhat like using an array (but less efficient in various ways).

Perl arrays are fairly efficient for implementing things like stacks and queues using push, pop, shift and unshift. They are also easy to access from either end - $array[-1] is the last element of a (non-empty) array for example. They are good for storing things where the order is important. Arrays are a poor choice if you need to search for stuff in them often.

Perl hashes are good for storing things you want to get at by name and where order is unimportant. They are an excellent choice where you need to determine if you already have an entry of a particular name already which often makes them the tool of choice when you need to look stuff up frequently.


True laziness is hard work

In reply to Re: Difference between array and a hash? (Newbie concerns) by GrandFather
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.