1. Home
  2. Computing & Technology
  3. Perl

Beginning Perl Tutorial - Data Types: Hashes
Accessing Hash Elements

by Kirk Brown
for About.com

Next: Readable Hash Initialization

Since each 'box' in this hash is identified by a specific key, we can access an individual value inside the box by using the key.

#!/usr/local/bin/perl
%contactInfo = ('name', 'Bob', 'phone', '111-111-1111');
print $contactInfo{'name'};
When you run the program above, you'll see it prints out Bob. In this example, 'Bob' is the value paired with the key 'name'. You'll also notice that (just like a list) to access a specific element of your %hash, you need to use a different method. Remember that lists and hashes are just groups of scalars, so if you want to access one of those scalars, you use the $scalar symbol, with the hash key contained in curly brackets:
$contactInfo{'name'}[1]
Next: Readable Hash Initialization
Explore Perl
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Perl Tutorials
  5. Beginning Perl Tutorial - Data Types: Hashes>

©2009 About.com, a part of The New York Times Company.

All rights reserved.