Arrays are good for doing array stuff and hashes are good for doing hash stuff. If you set aside how they work under the hood arrays and hashes are nearly identical (in PHP essentially they are identical). The "difference" between arrays and hashes is that arrays are indexed by numbers and hashes are indexed by strings.

Arrays are really good when you have a list of things you want to store and either they naturally are keyed by a number, or have no key but may be ordered. It's really fast to access element in an array by their index (position in the array). Perl arrays are also very efficient at adding and removing elements at the start and end of the array. Arrays tend to be a poor choice if there are large gaps where index values have been skipped.

Hashes are really good where you want to access values by name. Note that there is no reason the name can't be a number so in that sense hashes and arrays can do the same job. Although hashes are pretty quick at looking up values by name, they aren't as fast as arrays. The other major difference is that hashes don't remember the order that elements were inserted so they generally can't be used in a trivial fashion to store ordered data.

Unless you are generating a set of unique values by taking advantage of the fact that hash keys are unique, it seldom makes sense to use a hash to just store keys so on the face of it a hash doesn't make sense for storing a file system's structure because the file system doesn't allow duplicated names in any case. Nested arrays are a much better fit for a file system's structure.

Perl is the programming world's equivalent of English

In reply to Re: Trying to understand hashes (in general) by GrandFather
in thread Trying to understand hashes (in general) by james28909

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.