Home arrow Perl Programming arrow Page 5 - Perl Hashes

Alternative Methods for Creating Hashes - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

There are other ways to create hashes as well. In this next example we will use one of them. Note though that this only works for Perl 5 and later. Here, we will use the => operator to assign values:


#!/usr/bin/perl

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

',Nerd=>'James Payne');

print $HowItIs{Dumb};

This prints out:

  You

There really is no difference between this and the original method as far as I know. Some people say this way is easier to read, but it is really up to personal preference.

You could also create an empty hash if you wanted, and assign values to it later:


#!/usr/bin/perl

%HowItIs = ();

We'll learn how to add to it in a bit.

What if we want to create a hash from another hash? That too is a simple process:


#!/usr/bin/perl

%HowItIs = (Dumb=>'You ', Fat=>'YoMama ',UglyGenius=>'James Payne ',Nerd=>'James Payne ');

%HowItWas = %HowItIs;

print values(%HowItWas);

print "\n\n";

print keys(%HowItWas);

In the above example, we create our %HowItIs hash and assign it some values. Next, we create another hash named %HowItWas, and add the values from %HowItIs to it. We then print out first the values, then the keys to verify that it worked. The result:

  James Payne James Payne YoMama You

  UglyGeniusNerdFatDumb

If we only wanted to add a few of the values to our newly created hash, we could do so in the following manner:


#!/usr/bin/perl

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

',Nerd=>'James Payne ');

%HowItWas = @HowItIs{Dumb,Fat};

print values(%HowItWas);

print "\n\n";

print keys(%HowItWas);

The result?

  YoMama

  You

We aren't limited to creating just other hashes with our hashes. We can also create variables:


#!/usr/bin/perl

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

',Nerd=>'James Payne ');

$HowItWas = @HowItIs{Dumb};

print $HowItWas;


Which gives us:

  You

And we can also create lists as well:

#!/usr/bin/perl


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

',Nerd=>'James Payne ');

@HowItWas = @HowItIs{Dumb,Fat,Nerd};

print @HowItWas;

Here we are given:

  You YoMama James Payne

Conclusion

We covered a lot of ground in this article, however, there is much more to go. In our next tutorial we will learn to add records to hashes, remove them, check to see if a record already exists, create multidimensional lists, and much, much more. So be sure to stop by often.

Till then...



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

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