# 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.

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).

{mospagebreak title=Printing From Two-Dimensional Lists}

As you will recall from our discussion of lists, when you print elements within them you refer to them by their location within the list, or their element number. Printing from a two-dimensional list is not much different. Instead of printing from the lone element number, you reference both the row number and column number. It’s easiest to understand if you see it in action:

#!/usr/bin/perl

@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’]

);

print \$StuporHeroes[0][0];

Here we assign the values to our two-dimensional list, and then call one of them out by referring to its position on the grid. Can you guess what gets printed? Guess no more:

Mount Tittikanaka

This value is in the first row and first column (remember rows and columns begin at zero, just like list/array elements). For a better example, let’s print out the rest of the individual values:

#!/usr/bin/perl

@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 ‘]

);

print "nn";

print \$StuporHeroes[0][0] . "nt";

print \$StuporHeroes[0][1] . "nt";

print \$StuporHeroes[0][2] . "nt";

print \$StuporHeroes[0][3] . "nn";

print \$StuporHeroes[1][0] . "nt";

print \$StuporHeroes[1][1] . "nt";

print \$StuporHeroes[1][2] . "nt";

print \$StuporHeroes[1][3];

Before I show you the results, just note that I formatted the printing to give a better view of the elements:

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

{mospagebreak title=Printing an Entire Row from a Two-Dimensional List}

As you can see, printing out an entire row could prove to be a pain in the butt for larger databases. Imagine what would happen if we had a database where we stored a person’s first, middle, and last name, along with their pay rate, Social Security number, address, phone number, and so forth. Printing a single row would be quite an ordeal:

#!/usr/bin/perl

@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 ‘]

);

print "nn";

print \$StuporHeroes[0][0] . \$StuporHeroes[0][1] . \$StuporHeroes[0][2] .

\$StuporHeroes[0][3];

There is an easier way, however:

#!/usr/bin/perl

@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 ‘]

);

print "nn";

print @{@StuporHeroes[0]};

Though it looks weird in code, the line print @{@StuporHeroes[0]} simply says to print the list values that are in @StuporHeroes, from the row listed in the square brackets [].

This gives us the result:

Mount Tittikinaka Man-Girl Is a good listener Has Man-Boobs

There is no simple way to print a bunch of columns in the same manner.

{mospagebreak title=Using Two-Dimensional Lists to Create Variables and Lists}

Sometimes it is necessary to create an array from a value in a two-dimensional array. Maybe we want to see the secret lair of Man-Girl. Here is how we could do so:

#!/usr/bin/perl

@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 ‘]

);

print "nn";

\$Lair=\$StuporHeroes[0][0];

print \$Lair;

This code takes the value located in the first row and first column of @Stuporheroes, and stores it in \$Lair. We then print out the value, giving us:

Mount Tittikinaka

I could also store a complete row in a list like so:

#!/usr/bin/perl

@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 ‘]

);

print "nn";

@First = @{@StuporHeroes[0]};

print @First;

Just as in our earlier example of how to print an entire row, we specify which row to store in the variable in between the square brackets[]. In the above example, the result is:

Mount Tittikinaka Man-Girl Is a good listener Has Man-Boobs

To see how that two-dimensional list gets stored in a normal list, let’s try this code:

#!/usr/bin/perl

@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 ‘]

);

print "nn";

@First = @{@StuporHeroes[0]};

print @First[0];

Here we print the first element in the @First list, which we made from the first row of @StuporHeroes. Here is the result:

Mount Tittikinaka

For a better example, let’s print out each element in the list:

#!/usr/bin/perl

@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 ‘]

);

print "nn";

@First = @{@StuporHeroes[0]};

print @First[0] . "n";

print @First[1] . "n";

print @First[2] . "n";

print @First[3] . "n";

The result:

Mount Tittikinaka

Man-Girl

Is a good listener

Has Man-Boobs

And lastly, we can store the values in a hash as well:

#!/usr/bin/perl

@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 ‘]

);

print "nn";

%First = @{@StuporHeroes[0]};

print %First;

Returning the same result as above.

Well that is all the time we have for this episode. We didn’t get to all the topics we wanted to cover, but never fear; there are more articles to come. So be sure to check back often.

Till then…