Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The key to your dilemma lies in another term for hash: "Associative Array". A normal array is a series of items associated with a number indicating their position, like this:

my @array = ('one','two','three'); ## Results in: |___0___|___1___|___2___| | one | two | three |

A hash (associative array) is uses strings rather than numerical indexes as keys. In other words, instead of using an index number to locate the data, you associate a string "key" with each data value. This works especially well when you have 1:1 relationships in your data. For example, when I have a list of user IDs which each have a home directory associated with them:

my %hash = ( 'radiantmatrix' => '/home/2004/radiantmatrix/', 'somedude' => '/home/2009/somedude', ); ## results in |_radiantmatrix____________|_somedude___________| |/home/2004/radiantmatrix/ | /home/2009/somedude|

So, instead of saying $array[1] ("element of @array with index of '1'"), you can now use the association like $hash{'radiantmatrix'} ("element of %hash associated with the key 'radiantmatrix'")

You can build more complicated data structures by having, for example, hash keys which point to array references (or even other hash references). For example, if I have usernames and each user has a home directory and a phone number, I could do:

my %hash = ( 'radiantmatrix' => { home => '/home/2004/radiantmatrix/', phone => +'52123' }, 'somedude' => { home => '/home/2009/somedude', phone => '52124 +' } );

Now, when I ask for $hash{'somedude'}, the value is another hash (well, a reference to a hash). So, I can say $hash{'somedude'}{'phone'} ("the element with the key of 'phone', inside the element with the key of 'somedude', inside %hash", or "the 'phone' of 'somedude' from %hash").

So remember, you use a hash (associative array) when you have non-numeric keys (that is, strings) you want to associate with other data.

As a quick note, nothing prevents you from using numbers as hash keys, either, since Perl ignores the difference. Sometimes this is useful when you have numerical indexes, but there are big gaps in your data. If you have values to associate with the numbers 0,1,2,3,32000,32001 you could store that in a normal array, but perl will allocate memory for 32002 slots. By using a hash, you'd only use memory for values that you have an index for.

<-radiant.matrix->
Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
The Code that can be seen is not the true Code
"In any sufficiently large group of people, most are idiots" - Kaa's Law

In reply to Re: Use Hash? Array? Still confused after all these years. by radiantmatrix
in thread Use Hash? Array? Still confused after all these years. by hmbscully

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-16 23:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found