Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Use Hash? Array? Still confused after all these years.

by radiantmatrix (Parson)
on Jul 21, 2005 at 20:02 UTC ( [id://476980]=note: print w/replies, xml ) Need Help??


in reply to Use Hash? Array? Still confused after all these years.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://476980]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2024-03-29 07:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found