Perl
  Home arrow Perl arrow Page 2 - Perl: Sailing the List(less) Seas
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: Sailing the List(less) Seas
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-03-17

    Table of Contents:
  • Perl: Sailing the List(less) Seas
  • Printing Specific Elements
  • Slices
  • Replacing an Element Using a Slice OR Slice and Dice

  • 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: Sailing the List(less) Seas - Printing Specific Elements


    (Page 2 of 4 )

     Here is how we print only particular items from a list:

    #!/usr/bin/perl

    @gladiators=('Nitro ', 'Blaze ', 'CountFistula ','TheNutcracker ');

    print "These gladiators will mess you up: \n\n";

    print @gladiators;

    print "\n\n";

    print "My favorite gladiator is ";

    print $gladiators[2];

    print "\n\n";

    print "My most feared gladiator is ";

    print $gladiators[3];

    This gives us the result:

      These gladiators will mess you up:

      Nitro Blaze CountFistula TheNutcracker

      My favorite gladiator is CountFistula

      My most feared gladiator is TheNutcracker

    Each element in a list has an index number, starting with zero. In this instance, 'Nitro' is at index 0 and 'TheNutcracker' is at index 3. There are four elements in total within the array.

    Another way we can print individual elements in an array is like this:


    #!/usr/bin/perl

    @gladiators=('Nitro ', 'Blaze ', 'CountFistula ','TheNutcracker ');

    print "I made up these two gladiators: ";

    print $gladiators[2] . "\t" . $gladiators[3];

    Giving us:

      I made up these two gladiators: CountFistula        TheNutcracker

    You may have noticed that when I wanted to print single elements from the list I switched back to the $ symbol. This is because individually, the items in the list are scalar variables. All of the elements in a list combined and separated by a comma are known as literals. That's just a little reference material for you; it always helps to know the lingo.

    I know this may be a little confusing at the moment, but maybe this next section will clear the scalar/literal thing up a bit.

    More Perl Articles
    More By James Payne


       · Thank you for stopping by to read my article on Perl Lists. In this article we...
     

       

    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





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