Perl
  Home arrow Perl arrow Page 3 - 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  
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? 
Google.com  
PERL

Perl: Sailing the List(less) Seas
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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: Sailing the List(less) Seas - Slices
    ( Page 3 of 4 )

    A slice is a range of elements in a list. If it is only one element in a list, then it is known as a scalar slice and you refer to it with the $ symbol. If the slice has more than one element you use the @ symbol, because technically, a multi-element slice is itself a list.

    Let's take another look at our list of gladiator names. Of those names, I would like to specify that one is the champion. I am going to create a new variable named $champion and assign it the value of the champion's name. Here it is in code:


    #!/usr/bin/perl

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

    $champion=$gladiators[2];

    print $champion;

    Here we have stored the name of our champion in the variable. When we print it out we get the following result:

      CountFistula

    Now what if we had some tag team champions and wanted to add them to a new list? Remember that there are two values now, and it is no longer a scalar, so you switch back to the @ symbol:


    #!/usr/bin/perl

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

    @tagchamps=@gladiators[2,3];

    print @tagchamps;

    This will print out:

      CountFistula TheNutcracker

    Just for kicks, go ahead and try this code (I replaced the @ symbols with $ symbols):


    #!/usr/bin/perl

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

    $tagchamps=$gladiators[2,3];

    print $tagchamps;

    This will print out:

       The NutCracker

    Perl only takes one of the values, as it sees the $ symbol and is only expecting a scalar and not a list.

    Printing the Number of Elements in a List

    This method is pretty simple, and hardly requires its own section, with a pristine heading and all, but what the heck. If you want to know how many elements are in a list, you can do so in the following manner (just remember to add one to the result, as the result is just the number of the last element in the array, and not really the full length of the array):


    #!/usr/bin/perl

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

    print $#gladiators;

    Here we have the result:

      3

    Remember: element indexes begin with 0, so we add one to the number for our true number of elements. Although really, we could just use the following code and not worry about remembering a thing at all:


    #!/usr/bin/perl

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

    print $#gladiators+1;

    Giving us:

      4



     
     
    >>> 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 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek