Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

i tried using variables set to numbers to get values out of arrays and it seemed to work. is that different from hashes?
Yes :-). A longer answer is that hashes use variables set to almost anything you like to set or get values in something that is a bit like an array.

and i would think the program would find the values faster if the keys are ordered like the indices in arrays.
The way values are stored in hashes is in a sort of order.

Whilst hashing implementations vary, what normally happens is that a hashing function translates the key into an index element which refers you to a bucket, which is a short list of possible matches. A hash is often (but not always) faster than an array because you do not have to search through all the elements to find what you are looking for - you only have to check a few elements, and often only one if the hashing function is effective. Hashes are actually (sort of) an array, and what is hidden from you is how the "key" is translated into an "index" into that array.

Most of your questions are answered by Googling for hashes and looking for the explanations. This article is a bit dated but provides a good starting point. At the end of the article are a series of book references - if you are seriously interested in programming you should certainly get hold of the Donald Knuth book(s) temporarily or permanently, as he is the father of a huge number of computer algorithms and methods that are still in use today.

Hashes are used for much more than trivial lookups. Another article shows that hashes can be used imaginatively for lots of different purposes. Searching through some PerlMonks answers will also reveal just how useful hashing can be when used in a creative {and in certain PerlMonks answers, some would say nefarious :-)} fashion.

If you spot any bugs in my solutions, it's because I've deliberately left them in as an exercise for the reader! :-)

In reply to Re: why use a hash instead of an array by space_monk
in thread why use a hash instead of an array by DarrenSol

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [jdporter]: erix - excellent. thanks! davido - no; just reminiscing. :-)

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (7)
    As of 2018-06-21 17:02 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (118 votes). Check out past polls.