alvin's blog

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.

Perl hash remove - How to remove an item from a Perl hash

Perl Question: How do I remove an item from a hash?

Answer: Use the Perl delete function.

The general syntax you need to use is shown here:


If you'd like more details and examples, read on...

Perl hash - remove/delete element example

Here's a complete example where I show both how to create and print a Perl hash, and then show how to remove elements from the hash using the Perl delete function:

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:

How to add an item to a Perl hash

Perl hash FAQ: How do I add an item/element to a Perl hash?

Answer: Adding an item to a Perl hash is just like creating a Perl hash initially. Just assign the new item to the hash with the correct syntax, and you're in business.

In the following sample code I show how this is done by creating a hash named prices. The hash key is the name of a product, like pizza, and the hash value is the price of that product. Here's my Perl hash sample code:

What's new: Apple iPhone 2.1 software update bug fixes

I was really surprised by the installation process of the Apple iPhone 2.1 software update. From my experience with Apple and Mac OS X, their installation process is usually extremely vague, essentially saying "There's an update for your software, and we recommend that you do this upgrade", and few other details are offered, even if you follow a URL they provide.

Deploy only your JSP files with this Ant build task

A lot of times when you're working on a Java web application you only need to deploy your JSP files. This happens, for instance, when you're just editing the JSP files to modify the look and feel of your web application. In cases like this there's no need to rebuild your entire application, deploy it, then restart your application server (Tomcat, Glassfish, JBoss, whatever).

Ant FAQ: Ant replace task examples

Ant replace FAQ: Can you share some examples of the Ant replace task?

I've been sharing a lot of Ant tasks lately, and here's another example, this time some Ant replace task examples.

Ant replace task examples

The Ant build script lines shown below demonstrate how to issue a series of Ant replace commands to replace a token in the file with a variable you want to substitute for that token:

Ant FAQ: Ant copy task examples

Java Ant build FAQ: Can you share some examples of the Ant copy task, i.e., the Ant copy task syntax?

Sure. I've shared quite a few Ant examples on the website now (just search the website for "Ant"), and here's another snippet of code from an Ant build script that shows how to use the Ant copy task.

As you can see from this sample code, I'm using the Ant copy task to copy files from source locations to other destination locations.

Syndicate content