Sounds like you're having trouble understanding the concept of arrays and hashes. Without that understanding, coding with either could get confusing.

An array is simply an ordered list of data (string, integers, etc.). Each element in that list has an integer index. Some languages start their indexing at 1, but Perl starts at 0. When you're wanting an ordered list and are not concerned what each element is representing, arrays work great.

Let's say you wanted to store employee data in array. You could index 0 as employee ID, index 1 as last name, and so on. The challenge here is that you'll have to remember the what index represents what. In this case, hashes work better.

In a hash, you have a list of data. However, instead of identifying the elements by a numerical index, you use a key, which is basically a string. In the employee example above, you can use keys such as Employee_ID, Last_Name, First_Name, etc. Now in your code you're using text that clearly identifies what the element is.

With both hashes and arrays, you're dealing with pairs: an identify and it's data. In one case, the identify is an integer (arrays) and the other uses text.

What I've described so far has been single dimension implementations. In any array or hash, the data component of a element pair can be a new array or hash. If you have hash(es) inside of an array, that's called an array of hashes. If you have array(s) inside of a hash, that's called a hash of arrays.

Hopefully that helps you understand the general concepts of arrays and hashes. Once you've got the general concepts down, check out perlfaq4 for more information on how to use arrays and hashes in Perl. You can check out perllol and perldsc for more information about using more complex array and hash structures.


In reply to Re: Printing out a hash in specified format by dasgar
in thread Printing out a hash in specified format 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.