[Za-pm] Hashes
Nico Coetzee
nico at itfirms.co.za
Thu Jun 5 12:45:53 CDT 2003
On Thursday 05 June 2003 12:24, Theunis De Klerk wrote:
> Hi Everyone,
>
> I'm a newbie, especially to Perl.So could someone please explain what
> hashes are and what they are used for. From what i see they are sort of
> like multi demensional arrays . Is that right?
>
> Muchly appreciated!!!
> Theunis
>
<snip>
Think of them as a big cupboard with many drawers. Each label on a drawer is
an index, and if I open that drawer, I get the contents or value.
Let's say our cupboard holds personal info of various people, like their
e-mail address. The trick is that the index of a hash, must be the pipece of
information that uniquely identifies that data. In our example, names can be
duplicated - I know I share my name with some judge, and one or two other
"Nico Coetzee's" out there. What uniquely identifies me, is my e-mail
address, for example, nico at whereever.com. I will then use the e-mail as the
index:
$people{ "nico at whereever.com" } = "Nico Coetzee";
Because we now have an index, we can use that same index to store other bits
of info in other cupboards, or hashes:
$cellnr{ "nico at whereever.com" } = "000 000 0000";
Each time, we just add another person's data, by using his/her e-mail as the
index. In the end, I can then get a list of everybodies cell nr's like this:
foreach $key ( keys %people ) {
$name = $people{ $key };
$cell = $cellnr{ $key };
print "$name => $cell\n";
}
We are now looping through the hash, using the keys of the hash as a
reference. With each loop, we use the value of the $key variable to extract
the information from the hashes - this is optional, and you could combine it
all in one row, but for me personally it sometimes makes more sense to do it
this way ( logically ).
Better look out for that articles :)
Hope this helps somewhat
Cheers
--
Nico Coetzee
http://www.itfirms.co.za/
http://za.pm.org/
http://forums.databasejournal.com/
To the systems programmer, users and applications serve only to provide a
test load.
More information about the Za-pm
mailing list