Perl
  Home arrow Perl arrow Page 3 - Perl: More on Lists and Hashes
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: More on Lists and Hashes
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-05-27

    Table of Contents:
  • Perl: More on Lists and Hashes
  • How to Add Rows to a Two-Dimensional List
  • Adding a Column to a Two-Dimensional List
  • Adding a Column to Individual Rows in a Two-Dimensional List

  • 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: More on Lists and Hashes - Adding a Column to a Two-Dimensional List


    (Page 3 of 4 )

    Just as you can add a row to the end of a list, you can also add a column, though in truth it is a little more complex. In the sample below we are going to add a column to the end of each row. It will hold a value of “READ”, meaning of course that we have read the book. You may recall from an earlier article how to use the for loop. If not, don't worry too much about it. Shortly we will be going more in-depth on loops (in another article or two in fact). Here is the awe-inspiring code:


    #!/usr/bin/perl

    @Bookshelf = (

    [" # ", " Author ", " Title ", " Genre ", " Rating ", " Location "],

    [' 1 ', ' Stephen King ', ' It ', ' Horror ', ' 5 ', ' Top '],

    [' 2 ', ' Clive Barker ', ' Imajica ', ' Horror ', ' 5 ', ' Top '],

    [' 3 ', ' Neil Gaiman ', ' American Gods ', ' Dark Fantasy ', ' 5 ',

    ' Top '],

    [' 4 ', ' Dean Koontz ', ' Tick-Tock ', ' Horror ', ' 1 ', '

    GarbageCan '],

    [' 5 ', ' Charles Bukowski ', ' Letters from a Dirty Old Man ', '

    Literature ', ' 5 ', ' Top '],

    [' 6 ', ' Chuck Pahluniak ', ' Fight Club ', ' Dark Fantasy ', ' 5 ',

    ' Middle ']

    );

    $NumRows = @Bookshelf;

    for($i = 0; $i < $NumRows; $i +=1)

    { $Bookshelf[$i][7] = "READ"}

    print @{@Bookshelf[5]};

    After creating our two-dimensional list and assigning values to it, we then create a variable called $NumRows and assign it the value of @Bookshelf. However, since a variable cannot contain a 2-D list, the number of rows is instead store in the variable. Which is exactly what we intended. Next we create a for loop that appends a new column with the value of “READ” to the end of each row. Finally, we print out the fifth row, just to ensure that it worked, giving us the result:

      5 Charles Bukowski Letters from a Dirty Old Man Literature 5 Top READ

    Of course we may not wish to automatically assign the same exact value to the end of each row, but the code for that is a little beyond the scope of this article.

    If you are curious as to how the for loop works, here is a break down: the $i=0 part is the counter variable. $i < $NumRows is the criteria. Basically this says do this loop while $i is less than $NumRows. Finally, we have the incrementer, $i +=1. This adds one to the counter variable each time through the loop. The statement: { $Bookshelf[$i][7] = "READ"} adds a column to a different row each time through the loop (with the value “READ”). The loop breaks when the value in $i is greater than the value in $NumRows.

    More Perl Articles
    More By James Payne


       · Welcome to the first part of what should be the final two articles in our series on...
     

       

    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