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

Using Two-Dimensional Lists to Create Variables and 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

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

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

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

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

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

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



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