Pages

Wednesday, August 7, 2013

Multi-Dimensional Arrays in Perl

Introduction to Arrays

A Perl array is a data type that allows you to store a list of items. You create them by assigning them to an array variable. The array variable is identified by a @ prefix. To define a list of dates, we do this:

@array = ('20020701', '20020601', '20020501');

This is a one-dimensional array.

Two-Dimensional Arrays

There will be times when you will want to have more than one-dimension for your array. This is often the case for DBI database reads. In our example, we can add a title and authors to each date. We can create arrays for each date:

From here, you can have a quasi-two dimensional table as long as you write a code to handle it that way. But in Perl, you can simplify this. Instead of pumping these into one list, you can put the references of these arrays to the list: