Home Perl Programming Page 2 - Perl: Dimensional Lists

# Printing From Two-Dimensional Lists - Perl

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

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 "\n\n";

print \$StuporHeroes[0][0] . "\n\t";

print \$StuporHeroes[0][1] . "\n\t";

print \$StuporHeroes[0][2] . "\n\t";

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

print \$StuporHeroes[1][0] . "\n\t";

print \$StuporHeroes[1][1] . "\n\t";

print \$StuporHeroes[1][2] . "\n\t";

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

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