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  
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? 
PERL

Perl: More on Lists and Hashes
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 3
    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:
      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: 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
     

       

    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
    Stay green...Green IT