SunQuest
 
       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  
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 
Actuate Whitepapers 
VeriSign Whitepapers 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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


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

       

    PERL ARTICLES

    - 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
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI





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