Dec 27, 2006

Basic #8, Hashes

Hashes are just like arrays, but instead of numbers, hashes use strings as the index.

Declaration 1:

%longday = ("sun" => "sunday",
"mon" => "monday",
"tue" => "tuesday",
"wed" => "wednesday",
"thu" => "thursday",
"fri" => "friday",
"sat" => "saturday",
);

arrays use
@, while hashes use %
the left hand side are the keys, the right hand side are the values.


Another way of populating hashes:

%month = ("jan","january","feb","february","mar","march");

the first character of each pair of strings is the key, while the subsequent is the value.



We can also create a hash just by creating a pair value:

$dog{'jrt'} = "Jack Russel Terrier";

Accessing a single hash value:

print $dog{'jrt'};

No comments: