Perl
  Home arrow Perl arrow 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 
 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

    Minimize the cost of deploying database applications. Advantage Database Server or Microsoft SQL Server – Which One is Right for You? Learn now!

    Perl Lists: More Functions and Operators
    (Page 1 of 5 )

    We left off covering the splice() function, which can be used for adding and removing elements from a list. We also spoke about four other functions: pop(), push(), shift(), and unshift(), which are all similar to the splice() function, just not as flexible. In this article, we'll start out by discussing the splice() function in more detail, and learning how to remove more than one element in a list.

    The last example we discussed in our previous article dealt with removing one element from a list. Here, we will preview briefly the arguments that make up the splice() function, and learn how to add and remove multiple elements in a list with them.

    As we stated before, here are the arguments that make up a splice() function: 

    • Push: used to add an item to the right side (or end) of a list

    • Pop: a horrible type of music whose name derives from the word popular. Also, used to remove an item from the right side (or end) of a list

    • Shift: used to remove an item from the left side (or beginning) of a list

    • Unshift: used to add an element to the right side (or beginning) of a list

    • Splice: used to remove and add elements whose location you specify

    Removing More than One Element in a List

    Removing more than one element in a list is pretty similar to removing just one. All you do is change two things: the number of elements you wish to use, and the variable where you will store the removed elements from a scalar to a list. Here it is in code:


    #!/usr/bin/perl

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

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

    print @KoolAidFlavors;

    print "\n\n";

    @bestflavor=splice(@KoolAidFlavors, 1,2);

    print @KoolAidFlavors;

    print "\n\n";

    print @bestflavor;

    Here, we have take two elements from the @KoolAidFlavors list and stored them in @bestflavor. Here is the result of the code:

      Cherry Watermelon

    More Perl Articles
    More By James Payne


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

       

    PERL ARTICLES

    - 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
    - Perl: Releasing Your Inner Textuality




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