Perl: HashMap

HashMap is the most powerful data structure available in any language. And is available in almost every language. Perl has one too and is called Hashes or Associative arrays.

To declare a hashmap, use a % sign as follows:

%testHashpMap = ();

Now, key/value pair can be added and accessed as follows:

$testHashpMap {$key} = $value;

Note, curly braces {} in the hashmap.

A small program that illustrates this:

%testHashpMap = ();

$testHashpMap {“delhi”} = “india”;
$testHashpMap {“london”} = “uk”;
$testHashpMap {“new york”} = “us”;
$testHashpMap {“tokyo”} = “japan”;

foreach $city ( keys %testHashpMap ) {
print STDERR “$city: $testHashpMap {$city} \n”;


Read complete Perl Tutorial.

We recommend: Perl Best Practices