home about us contact us

free scripts advanced scripts online tools great books web related tutorials contributed tutorials news archive geek toys!

help forum live chat help

Working with an array of hashes.

In this little tutorial snippet, I hope to show how to create one of the more advanced data constructs, namely an array (numbered list) of anonymous hashes (keyword lists without defining names). Following that I will hopefully show how to read, modify and "create new" elements into those hashes.

Before I get into this, I should point out that those of you not familiar with arrays and hashes should check out the other little tutorials I have done which are located: here (variables and arrays) and here (associative arrays or hashes) before continuing with this one, as you will need to understand the basic layouts of both arrays and hashes for this to make much sense.

Lets begin by creating an array of hashes to contain the details of a number of files that company X might be selling in a shopping cart script (for example).
The benefit of this, is that you can store all the data you need about these files in a construct that allows the storage of all the files attributes in a simple and managable manner, but more importantly, it allows you to extract any files attributes easily based on a search of any of the attributes that exist for that file.
In a way its alittle bit like using a database to fetch or manipulate data.
(for example, we can list all the attributes of a file, by searching the array for a particular price, (desc)ription or file name.)

     # First lets define an array of hashes.

my @file_attachments = (
       {file => '',  price  => '10.00',  desc  => 'the 1st test'},
       {file => '',  price  => '12.00',  desc  => 'the 2nd test'},
       {file => '',  price  => '13.00',  desc  => 'the 3rd test'},
       {file => '',  price  => '14.00',  desc  => 'the 4th test'}

     # Get the number of items (hashes) in the array.
my $file_no = scalar (@file_attachments);
     # $file_no is now: 4 in this instance as there is 4 hashes in the array.

     # Looping through the hash and printing out all the hash "file" elements.
for (my $i=0; $i < $file_no; $i++)
print '$file_attachments[$i]{'file'}  is:'. $file_attachments[$i]{'file'}."\n";

     # Looping through the hash and printing out all the hash "price" elements.
for (my $i=0; $i < $file_no; $i++)
print '$file_attachments[$i]{'price'} is:'. $file_attachments[$i]{'price'}."\n";

     # Looping through the hash and printing out all the hash "desc" elements.
for (my $i=0; $i < $file_no; $i++)
print '$file_attachments[$i]{'desc'} is:'. $file_attachments[$i]{'desc'}."\n";

     #The loops will return this output:

$file_attachments[0]{'file'} is:
$file_attachments[1]{'file'} is:
$file_attachments[2]{'file'} is:
$file_attachments[3]{'file'} is:

$file_attachments[0]{'price'} is: 10.00
$file_attachments[1]{'price'} is: 12.00
$file_attachments[2]{'price'} is: 13.00
$file_attachments[3]{'price'} is: 14.00

$file_attachments[0]{'desc'} is: the 1st test
$file_attachments[1]{'desc'} is: the 2nd test
$file_attachments[2]{'desc'} is: the 3rd test
$file_attachments[3]{'desc'} is: the 4th test

     #Loop through the array, and look for $testing, (which is set to

my $testing = '';
for (my $i=0; $i < $file_no; $i++)
        if ($file_attachments[$i]{'file'} eq $testing)
          print "$file_attachments[$i]{'file'} exists in the hash contained in element $i of the array.";
     # Which, because IS in one of the hashes the 'file' element, will return: exists in the hash contained in element 1 of the array.

To change the value of one of the inner hashes is really quite easy and should be obvious if you have read this far. It's just a simple case of assignment.
For example, lets change the description value of the third hash. (which since arrays always start at 0 would be the array item [2].)

   # Changing a value in one of the inner hashes..
$file_attachments[2]{'desc'} = 'This used to be the 3rd test';

Ironically that is the same way you would go about adding a new item/value to one of the hashes as well. If you do an assignment to a hash element that doesn't exist, Perl is nice enough to add it for you. In this little example, I will add a new element called location to the 4th hash in the array. [3].

   # Adding a new element to an inner hash...
$file_attachments[3]{'location'} = 'the location of the file might go here';

And there you have it, in this little snippet, we have seen how to create an Array of hashes, then we saw how to access specific items in the hash, how to change or add elements to the inner hashes, and we also saw how to test if a specific element exists in any of the arrays.

In my next primer, I plan to cover an even more complex, but potentially much more useful structure, this will be the "hash of hashes".
Are we having fun yet ? :-)

So if you are ready to learn more:
Back to the Tutorial Index