Perl
  Home arrow Perl arrow Page 2 - 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 - How to Add Rows to a Two-Dimensional List


    (Page 2 of 4 )

    Adding a row to a two-dimensional list is pretty simple. In this next example we are going to add a new book to our bookshelf using the push() function, which adds your new item to the end of the list (or the right-most side):


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

    );

    @NewBook = (' 7 ', ' T.H. White ', ' The Once and Future King ', '

    Fantasy ', ' 5 ', ' Middle ');

    push(@Bookshelf, [ @NewBook ]);

    print "\n\n";

    print @{@Bookshelf[7]};

    Here we create our 2-D list called @Bookshelf and assign a bunch of values to it. We then create a normal list called @NewBook and assign values to it. Next we use the push() function to place the values in the @NewBook list into our 2-D list, which it appends to the end. Finally, we print the newly added row. The result is:

      7 T.H. White The Once and Future King Fantasy 5 Middle

    Another way to add a row to a two-dimensional list is like this:


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

    );

    @Bookshelf[7]= (

    [' 7 ', ' T.H. White ', ' The Once and Future King ', ' Fantasy ', '

    5 ', ' Middle ']

    );

    print @{@Bookshelf[7]};

    This gives us the same result without using the push() function. Of course there is one other, shorter way to add a row:


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

    );

    push(@Bookshelf, [' 7 ', ' T.H. White ', ' The Once and Future King ','Fantasy ', ' 5 ', ' Middle ']);

    print @{@Bookshelf[7]};

    Again, it gives us the same result:

      7 T.H. White The Once and Future King Fantasy 5 Middle

    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 5 hosted by Hostway
    Stay green...Green IT