Variables
Using Associative Arrays
- An
associative list or hash table is useful for storing name-value pairs
- A
hash table variable is designated by the "%" prefix.
- There
are many ways to initialize a hash table variable:
- Initialize the hash as a list:
%days = ( 'M', 'Monday', 'T', 'Tuesday' );
- We can initialize
the hash table element-wise : $days{'W'}
= 'Wednesday';
- Again,
you can use any scalar data type for the key or for the value
$myConst{"PI"} = 3.14159;
$hg{42}
= "life, the universe, and everything";
- Accessing a value
from a hash is easy: print
"$days{'T'}\n";
prints
Tuesday to screen