Home arrow Perl Programming arrow Page 3 - Perl: Dimensional Lists

Printing an Entire Row from a Two-Dimensional List - 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 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 "\n\n";

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

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.



 
 
>>> 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: