Hashes

Accessing individual hash elements

Whereas array elements are accessed by their (numerical) index, hash elements (values) are accessed by their keys.

Syntax

Assuming that @array is some array and %hash is some hash:

ArrayHash
$array[2]$hash{"some_key"}
$array[$n]$hash{$k}

Example

#!/usr/bin/perl

%prices = ("shirt"    => 45,
           "pullover" => 90,
           "trousers" => 120,
           "socks"    => 15);

$s = $prices{"shirt"};
$t = $prices{"trousers"};


print "EXAMPLE PRICES:\n";
print "SHIRT: $s NIS,  TROUSERS: $t NIS\n";
Result:
EXAMPLE PRICES:
SHIRT: 45 NIS,  TROUSERS: 120 NIS

Table of Contents.
Previous | Next.