Perl
  Home arrow Perl arrow Page 5 - Perl Lists: More Functions and Operators
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 Lists: More Functions and Operators
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2008-03-31


    Table of Contents:
  • Perl Lists: More Functions and Operators
  • Removing Elements without Storing Them
  • Using Splice() to Add and Replace
  • Adding Values to a List with Splice()
  • A Few Operators

  • 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 Lists: More Functions and Operators - A Few Operators
    ( Page 5 of 5 )

    You can use some operators on lists in a surprising way. While we don't have time to cover them all here, I figured I would spend this time showcasing some of them for you. For instance, there is the + operator. Let's see what happens when we add a list to itself:


    #!/usr/bin/perl

    @KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon

    ','Fruit-Punch ','Orange ');

    print @KoolAidFlavors + @KoolAidFlavors;

    You may or may not (depending on how your brain works) expect this to result in:

      Grape Cherry Watermelon Fruit-Punch Orange Grape Cherry Watermelon Fruit-Punch Orange

    However, you would be wrong. Instead, the program counts the number of elements in the array and adds them together. Since there are five elements in our list, the result is:

      10

    Likewise, if we use the following code:


    #!/usr/bin/perl

    @KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon

    ','Fruit-Punch ','Orange ');

    print @KoolAidFlavors + 2;

    We get the result:

      7

    Or the number of elements, plus 2.

      You will note that the subtraction operator works in the same way:


    #!/usr/bin/perl

    @KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon

    ','Fruit-Punch ','Orange ');

    print @KoolAidFlavors - 2;

    Here the result will be 3, as 5 (the number of elements in the list) -2 is equal to 3.

    But what if we use the multiplication symbol (x, not *)? Here, we have a completely different result:


    #!/usr/bin/perl

    @KoolAidFlavors = (@KoolAidFlavors, 'Grape ','Cherry ','Watermelon

    ','Fruit-Punch ','Orange ') x 2;

    print @KoolAidFlavors;

    Now this will print the array twice. If we had put x3, it would print it three times and so forth. Note that I put the x2 at the end of the list. Had I put it after the print (and used * instead of x), then it would have simply multiplied he number of elements by 2 and returned the number 10. If I had put the x2 after the print command it would have returned 55, for reasons with which I am unfamiliar.

    The result of the above code is:

      Grape Cherry Watermelon Fruit-Punch Orange Grape Cherry Watermelon Fruit-Punch Orange

    Note that the value in @KoolAidFlavors is now all of the above. The code doesn't simply print out the value of the list twice; it stores it twice (or however many times you decide).

    Another operator we can use is the "..". This allows us to add sequential values to a list. It's best viewed in code:


    #!/usr/bin/perl

    @NumberList = (1 .. 20);

    print @NumberList;

    This will store the values from 1-20 in our list. When we print it out we get the result:

      1234567891011121314151617181920

    To further illustrate how this works, in the following code we assign the sequential values the same, but only print out the seventh element (remember that element number begin with 0):


    #!/usr/bin/perl

    @NumberList = (1 .. 20);

    print @NumberList[7];

    The result of this code is:

      8

     

    This also works with letters:

    #!/usr/bin/perl

    @Alpha = (a .. h);

    print @Alpha;

    print "\n\n";

    print @Alpha[3];

    This will print out:

      abcdefgh

      d

    Note that this will not work in reverse; I couldn't type in 10 .. 1 and expect it to return: 10987654321 etc.

    Conclusion

    We covered a lot of ground in this article. In our next tutorial in this series we will go over the split() function, the List::Util, and maybe, just maybe touch upon those hashes I've been promising. There's only one way to find out though, and that's to drop by as often as possible. So do that.

    Till then...



     
     
    >>> 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 2 Hosted by Hostway
    Stay green...Green IT