Perl
  Home arrow Perl arrow Page 5 - Perl Lists: More Functions and Operato...
Dev Shed Forums 
Administration  
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
IBM Rational Software Development Conference
 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: 5 stars5 stars5 stars5 stars5 stars / 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:
      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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    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...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Thanks for stopping by for another article in our series on Perl Lists and Hashes....
     

       

    PERL ARTICLES

    - 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
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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