Home arrow Perl Programming arrow Page 3 - Perl Hashes

Printing from a Hash - Perl

Thanks for stopping by for this seventh part in the series on Perl Lists and Hashes. Here we will discuss working with a new way to store data known as a Hash. We'll cover the basics, such as how to create them, and how to locate data within them. You'll learn to add records, remove records, and much more. If there is time, we will also cover multidimensional lists.

  1. Perl Hashes
  2. Creating A Hash
  3. Printing from a Hash
  4. Printing All the Values or Keys in a Hash
  5. Alternative Methods for Creating Hashes
By: James Payne
Rating: starstarstarstarstar / 6
April 28, 2008

print this article



There are several print methods that you can use with hashes. The first we will learn about is printing individual values from a hash. Here, we will use our previous list of different types of brownies to print from:


%Brownies = (1,'Chocolate ', 2,'Fudge ',3,'Vanilla ',4,'Peanut Butter


print $Brownies{1};

You will note that we use $Brownies in our print statement instead of %Brownies. This is because we are only printing a single value, and thus it becomes a variable. The result of this is:


If we wanted to print more than one value from the hash, we would change the $Brownies variable to @Brownies, since there will be more than one value in it. Here is how we return two or more values:


%Brownies = (1,'Chocolate ', 2,'Fudge ',3,'Vanilla ',4,'Peanut Butter


print @Brownies{1,2};

This simplistic code returns the following:

  Chocolate Fudge

Note that the keys that refer to your values don't have to be numbers. Here, we will use some text instead:


%HowItIs = (Dumb,'You ', Fat,'YoMama ',UglyGenius,'James Payne

',Nerd,'PHP Programmers');

print @HowItIs{Dumb,Fat,Nerd};

This code assigns the value “You” to the key Dumb, “YoMama” to Fat, and so forth. When we run this program, it prints the following:

  You YoMama PHP Programmers

Another thing to remember is that your keys may not equal the same value, but the value inside your keys can. Let's say that in addition to being an ugly genius, I am also a nerd. Here is how I could assign myself to two keys:


%HowItIs = (Dumb,'You ', Fat,'YoMama ',UglyGenius,'James Payne

',Nerd,'James Payne');

print @HowItIs{UglyGenius,Nerd};

The result is of course:

  James Payne James Payne

>>> More Perl Programming Articles          >>> More By James Payne

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: