Home arrow Perl Programming arrow 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.

TABLE OF CONTENTS:
  1. Perl: Dimensional Lists
  2. Printing From Two-Dimensional Lists
  3. Printing an Entire Row from a Two-Dimensional List
  4. Using Two-Dimensional Lists to Create Variables and Lists
By: James Payne
Rating: starstarstarstarstar / 2
May 19, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: