perl hash

The Perl exists function - test to see if a hash key exists

Perl "hash key exists" FAQ: How can I test to see if a key exists in a Perl hash?

Many times when working with a Perl hash, you need to know if a certain key already exists in the hash. The Perl exists function lets you easily determine if a key already exists in the hash.

My Perl hash tutorials

Perl hash tutorials FAQ: Can you share some Perl hash tutorials, or Perl hash examples?

I've recently written a number of articles about the Perl hash construct (or "Perl array hash"). In an effort to try to organize the Perl hash tutorials I've written, I've created this article to help link them all together.

Perl hash size - How to get the size of a hash in Perl

Perl hash size FAQ: How do I get the size of a hash in Perl?

Short answer: To get the size of a Perl hash (the Perl hash size), use the Perl "keys" function, and assign it to a scalar value, like this:

Perl hash - delete example

Perl hash delete FAQ: How do I delete an element (a key/value pair) from a Perl hash? Also, how do I delete multiple Perl hash elements at one time?

A Perl hash delete example - Delete an element by hash key

To delete a Perl hash element, use the Perl "delete" function. The general syntax of the Perl delete function looks like this:

Perl hash add element - How to add an element to a Perl hash

Perl hash "add" FAQ: How do I add a new element to a Perl hash? (Or, How do I push a new element onto a Perl hash?)

The Perl hash is a cool programming construct, and was very unique when I was learning programming languages in the late 1980s. A Perl hash is basically an array, but the keys of the array are strings instead of numbers.

Basic Perl hash "add element" syntax

To add a new element to a Perl hash, you use the following general syntax:

Perl hash introduction tutorial

Perl hash FAQ: Can you share some simple Perl hash examples?

Sure. If you're not familiar with them, a Perl hash is simply a Perl array that is indexed by a string instead of a number. A Perl hash is like a Map in the Java programming language, or an array in PHP.

Perl hash - Background information

To get started looking at a hash in Perl, let's look at a simple example. First, let's assume that Perl hashes don't exist. Next, lets assume that we need to store the prices of various food items you'll find in a restaurant.

Perl hash sort - How to sort a Perl hash by the hash key

Perl hash sort FAQ: How do I sort a Perl hash by the hash key?

Answer: Sorting the output of a Perl hash by the hash key is fairly straightforward. It involves two Perl functions, keys and sort, along with the good old foreach statement.

Perl hash foreach and while - How to loop over a hash in Perl

Perl hash question: How do I traverse the elements of a hash in Perl?

Answer: There are at least two ways to loop over all the elements in a Perl hash. You can use either (a) a Perl foreach loop, or (b) a Perl while loop with the each function. I find the Perl foreach syntax easier to remember, but the solution with the while loop and the each function is preferred for larger hashes.

A Perl hash print example (printing hash elements)

Perl hash question: How do I print the elements stored in a hash in Perl?

Answer: There are at least two ways to print the elements of a Perl hash, including one way that I think is easy to remember, and another way that is recommended if you have a very large hash.

Perl hash printing with a foreach loop

The easier way for me to remember is with a Perl foreach loop. In the following sample code I'll first create a Perl hash, and then I'll print out each key and value stored in the hash: