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 @arr is some array and %assoc is some hash:

ArrayHash
$arr[2]$assoc{"some_key"}
$arr[$n]$assoc{$k}

Example

#!/usr/local/bin/perl
use strict;
use warnings;

my (%prices, $s, $t);

%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.
Next.