Home arrow Perl Programming arrow Page 2 - Perl Hashes

Creating 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



If you are familiar with other programming languages, then you will know that a hash is similar to an associative array. Before we create our first hash, there are a few rules you must learn. The first one is the naming convention. Hashes are named like other lists, except that they start with a percent sign (%) rather than the ampersand (@). When you refer to a key you use the curly braces {}, as opposed to parentheses as in regular lists. When you create the hashes, you use parentheses. Okay, enough blabbering; let's create our first hash:


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

In the above example we created four key-value pairs. If we printed out the hash, this is how we would do so, and also, the result:


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

print %Brownies;

Which prints:

  4Peanut Butter1Chocolate3Vanilla2Fudge

Not quite what we expected right? You will note that hashes are not ordered lists and will not necessarily print in the order you enter them. Also, you will notice that the key gets printed along with the value when we print in this manner. We will learn in a short bit how to print only the values when we want to print the entire hash.

>>> 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: