Perl
  Home arrow Perl arrow Page 4 - Perl: A Continuing Look at Hashes and ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PERL

Perl: A Continuing Look at Hashes and Multidimensional Lists
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-05-12

    Table of Contents:
  • Perl: A Continuing Look at Hashes and Multidimensional Lists
  • Using If Statements to See if a Record Exists
  • How To See If A Specific Record Exists
  • Molemen...From the Second Dimension!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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: A Continuing Look at Hashes and Multidimensional Lists - Molemen...From the Second Dimension!


    (Page 4 of 4 )

    Two-dimensional lists are lists that have other lists as their items. Go ahead. Grab that mop and clean up the mess I just made by making your head explode. In the next few examples, we are going to pretend that we have a database of lame superheroes that just never made it. Each record will consist of the following:


    • Secret Lair

    • Name

    • Super power

    • Super weakness

    Let's go ahead and create our first two-dimensional array:


    #!/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']

    );

    In the above example, we created a two-dimensional list named @StuporHeroes. You will note that even though our list has an added dimension, we still use the @ symbol before the name, just as with our normal lists. We then encase the values in our two-dimensional list with square brackets, instead of parentheses as per normal lists. Each piece of data or “cell” is separated by a comma. Each row of data is placed between a set of parentheses, ending in a semi-colon, as shown above.

    If this seems a little confusing at the moment, that is all right. When I explain them in the next section everything will become crystal clear.

    Better Understanding the Two-Dimensional List

    The best way to think of a two-dimensional list is to imagine an Excel sheet. I used that same analogy with hashes, but really it applies more to this storage device. For instance, in a spreadsheet you enter data into a grid, like the one shown below:



    As you can see, the value Spiderman is located at row 3, column A. Batman is row 5, column A, Thanos is row 8, column B, and so forth. This is how Perl sees your record. In our sample code above for instance, we assigned our lists values like this:

    @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']

    );

    Our first row contains all of the following data: “Mount Tittikanaka”, “Man-Girl”, “Is a good listener” and “Has man boobs”. Note that with lists, element numbers start at 0. All of these elements have a row number of zero. However there column number differs. If I wanted to call on “Mount Tittikanaka” for instance, I would search for the value in row 0, column 0. If I wanted “Man-Girl”, I would search in row 0, column 1, and so forth.

    The second row contains the values: “Trailer Park”, “Deaf Leapard”, “Has a super sonic guitar”, and “Is deaf and has one arm making him unable to play his guitar”. If I wanted to refer to “Trailer Park” I would point to row 1, column 0. “Deaf Leopard” is located at row 1 and column 1. Always keep in mind that elements start at 0 (as do our rows and columns).

    Well that's it for this article. In our next episode we will learn to print from our extra-dimensional lists, and figure out other ways to manipulate the data contained within them. So be sure to stop back often!

    Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Thanks for stopping by to read another installment of my series on Lists and Hashes....
     

       

    PERL ARTICLES

    - 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
    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
    Stay green...Green IT