Perl Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 5.10

Using Hash Variables

This section describes how a hash variable can be assigned with a list value. Hash elements are referred by key subscription notation as scalar expressions, like $identifier{'key'}. An unassigned hash variable contains the (undef) value.

Hash variables can be used in a Perl script under these rules:

  • An hash variable should be assigned with a list value constructor using an assignment operation. Every 2 elements in the list value will be formed into a pair of key and value into the hash variable. If there is an extra key at the end of the list without a value, this key will have no value assigned. For example: %siteRanks = ("herongyang.com", 4, "perl.org", 6, "google.com", 9).
  • To increase legibility, the => operator can be used to link a value to the key in the list value constructor. For example: %siteRanks = ("herongyang.com" => 4, "perl.org" => 6, "google.com" => 9).
  • Each value in a hash variable can be referenced by a scalar expression with the key subscription notation, $identifier{"key"}. The "key" is the string value of the key linked to the value in the hash. For example: $siteRanks{"herongyang.com"} is a scalar expression referring to the value linked to the key "herongyang.com" in the hash variable.
  • If a new value is assigned to a new key in the hash variable, a new pair of key and value will be added into the hash variable.
  • If you are trying to access a value with a key that has no value assigned, you will get the undef value.

Here are some examples of hash variable assignment operations with list value constructors:

   %messages = 'hello'; 
   %primes = 3,5,7,11;
   %vacations = 'Mon','Tue','Wed';
   %vacations = ('Mon','Tue','Wed');
   %vacations = (Mon,Tue,Wed);
   %menu = 'Mon',2,'Apple';
   %dates = 'Jan',31,'Feb',28,'Mar',31;
   %prices = ('Milk',1.99,'Coke',0.99,'Bread',1.49);

Here is a Perl tutorial script on how to use array variables:

#- HashVariable.pl
#- Copyright (c) 1995 by Dr. Herong Yang, http://www.herongyang.com/
#

   %siteRanks;                         # Undefined
   print(%siteRanks, "\n");

   %siteRanks = ("herongyang.com", 4, "perl.org", 6, "google.com", 9);
   print(%siteRanks, "\n");            # Defined now

   %siteRanks = ("herongyang.com"=>4, "perl.org"=>6, "google.com"=>9);
   print(%siteRanks, "\n");            # Using => operator

   $myRank = $siteRanks{"herongyang.com"}; 
   print($myRank, "\n");               # Refers to a value by key

   $siteRanks{"google.com"} = 10;      # Updates a value by key
   print($siteRanks{"google.com"}, "\n");

   $siteRanks{"yahoo.com"} = 8;        # Adding a new key-value pair
   print($siteRanks{"yahoo.com"}, "\n");

   print ("undef", "\n") if ($siteRanks{"xyz.com"} == undef);
                                       # Undefined key

Here is the output of the tutorial script:


herongyang.com4google.com9perl.org6
herongyang.com4google.com9perl.org6
4
10
8
undef

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

Data Types: Values and Variables

 Scalar Values and List Values

 Scalar Value Constructors

 Scalar Value Interpretation

 List Value Constructors

 Variables - Scalar, Array and Hash

 Using Scalar Variables

 Using Array Variables

Using Hash Variables

 "undef" Value and Undefined Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Converting Perl Script to Executable Binary

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

 RPC::XML - Perl Implementation of XML-RPC

 Integrating Perl with Apache Web Server

 References

 Printable Copy - PDF Version

Dr. Herong Yang, updated in 2009
Using Hash Variables