there are many good examples of code here. I will not presume to put another. What I will do is try and explain what a hash is a bit more in detail because I think (like I once was) you are really struggling with that. Before I get going, nay I suggest a book that really helped me a year ago: Elements of Programming with Perl. The link is to my review of it. It's a great hands on way to get to know perl end to end.

in perl programs there are three ways to store the data you want to throw around, scalars, arrays and hashes. scalars, as you know by now, are the simplest. you put something in and you take it out in the same form. basically it just holds what you put in there. an array will take a whole bunch of things and order them for you. you give it a list and it will hole all the elements and give you whichever you want when you ask for it. if you want the 3rd item you placed in you simple ask for the item which is associated with the index [2](because the first is always 0!), and you will get it.

a hash is something you can think of as an array with a few different features. in fact, another name for a hash is an associative array. basically it is an unordered array which associates it's elements with names instead of numbers. above we said we ask for elements from an array with their associated numbers or indexes. with a hash, you would ask for things by their name, called a "key". i also said that a hash is unordered. this means that unlike an array it does not keep things in the order in which you put them in. perl decides what order to keep thing in according to black magic in the guts which optimizes the storage and retrieval of the data in the hash. let's look at some simple stuff to illustrate this.

first of all, proving how like an array a hash is, you can form one just like you would an array:

@array = ( one, monkey, two, donkey, three, funky ); # makes an array %hash = ( one, monkey, two, donkey, three, funky ); # makes a hash where each of one, two and three is a key # for the data to their right, but better written as %hash = ( one => monkey, two => donkey, three => funky ); # in both ones, two, three are keys and the rest the values. # the *only* difference is that the second is clearer for a # human. => has no special meaning at all - just another # comma for all perl cares. print $array[1]; # will print monkey print $hash{one}; # also prints monkey # notice both have stored simple scalers # so when we get the info for the print # we ask for a scalar.

what you store in a has doesn't have to be a scalar. you can store whole arrays, through references (another one I struggled with), and even other hashes.

I hope you find this useful. I also hope you don't see it as me talking down to you in any way. I struggled with this really hard a year ago. with no math or comp sci background all the references I found were leaving me spinning. i just hope to help you avoid that =)

"A man's maturity -- consists in having found again the seriousness one had as a child, at play." --Nietzsche

In reply to (jptxs) Re: Hash Variable by jptxs
in thread Hash Variable by qball

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.