Getting Data Out of a Hash

As you have seen, to retrieve a single value from a hash, simply use a $, the name of the hash, and (in curly braces) the key whose value you want to retrieve. Now consider this example:

%Movies = ( 'The Shining' => 'Kubrick', 'Ten Commandments' => 'DeMille', Goonies =>
 'Spielberg');
print $Movies{'The Shining'};

These lines print the value associated with the key The Shining in the hash %Movies. This example would print Kubrick.

Sometimes examining all the elements of a hash is useful. If all the keys of the hash are known, you can access them individually by key as shown previously. Most of the time, though, accessing ...

Get SAMS Teach Yourself Perl in 24 Hours THIRD EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.