Home Perl Programming Perl: Dimensional Lists

# Perl: Dimensional Lists

Thanks for stopping by to read my tenth installment on working with hashes and lists in Perl. In our previous article we learned how to find out if a hash holds a specific value, does not hold a specific value, or holds any value whatsoever. We further learned to create our first two-dimensional array and then worked to better understand how exactly a two-dimensional array works. Which is where we pick up in this article.

By: James Payne
Rating:  / 2
May 19, 2008

SEARCH DEV SHED

TOOLS YOU CAN USE

In addition to completing our understanding of how two-dimensional arrays work, we will also learn how to print from them, add, replace, and delete from them, and discuss going beyond just the two dimensions. It is a lot to cover, so let's begin.

For the sake of continuity I am going to briefly describe how Two-Dimensional arrays work again (so you don't have to seek out the other article if you need a refresher). If you remember how they work, or already know, then feel free to skip this section and move on to the next. For those of you interested, keep doing what you are doing; read on.

Better Understanding the Two-Dimensional List

The best way to think of a two-dimensional list is like an Excel sheet. I used that same analogy with hashes, but really it applies more to this storage device. For instance, in a spreadsheet you enter data into a grid, like the one shown below:

As you can see, the value Spiderman is located at row 3, column A. Batman is row 5, column A, Thanos is row 8, column B, and so forth. This is how Perl sees your record. In our sample code above for instance, we assigned our lists values like this:

@StuporHeroes = (

[' Mount Tittikanaka ', ' Man-Girl ', ' Is a good listener ', ' Has

Man-Boobs '],

['Trailer Park', 'Deaf Leapard', 'Has a super sonic guitar', 'Is deaf

and has one arm making him unable to play his guitar']

);

Our first row contains all of the following data: “Mount Tittikanaka,” “Man-Girl,” “Is a good listener” and “Has man boobs.” Note that with lists, element numbers start at 0. All of these elements have a row number of zero. However, their column numbers differ. If I wanted to call on “Mount Tittikanaka” for instance, I would search for the value in row 0, column 0. If I wanted “Man-Girl,” I would search in row 0, column 1, and so forth.

The second row contains the values: “Trailer Park,” “Deaf Leapard,” “Has a super sonic guitar” and “Is deaf and has one arm making him unable to play his guitar.” If I wanted to refer to “Trailer Park” I would point to row 1, column 0. “Deaf Leopard” is located at row 1 and column 1. Always keep in mind that elements start at 0 (as do our rows and columns).

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