Perl
  Home arrow Perl arrow Page 4 - Perl: Dimensional Lists
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Perl: Dimensional Lists
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-05-19


    Table of Contents:
  • Perl: Dimensional Lists
  • Printing From Two-Dimensional Lists
  • Printing an Entire Row from a Two-Dimensional List
  • Using Two-Dimensional Lists to Create Variables and Lists

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Perl: Dimensional Lists - Using Two-Dimensional Lists to Create Variables and Lists
    ( Page 4 of 4 )

    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 Articles          >>> More By James Payne
     

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek