1. Technology

Beginning Perl Tutorial - Data Types: Hashes

Accessing Hash Elements

By

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
  1. About.com
  2. Technology
  3. Perl
  4. Perl Tutorials
  5. Beginning Perl Tutorial - Data Types: Hashes

©2014 About.com. All rights reserved.