Perl
  Home arrow Perl arrow Page 3 - 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 - Using Splice() to Add and Replace
    ( Page 3 of 5 )

    We can also use splice() to add and replace elements in an array. Let's say we want to get rid of two flavors from our list, and replace them with two others:


    #!/usr/bin/perl

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

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

    @rem = ('Pomegranate ', 'Blueberry ');

    print @KoolAidFlavors;

    print "\n\n";

    splice(@KoolAidFlavors, 1,2,(@rem));

    print @KoolAidFlavors;

    print "\n\n";

    This code creates the @KoolAidFlavors list, adds a value to it, and then creates the @rem list, adds values to it, and prints out the value of KoolAidFlavors. Next we use splice() to replace the two elements following the first element with the values in the @rem list. When we run this program, we get the following print-out:

      Grape Cherry Watermelon Fruit-Punch Orange

      Grape Pomegranate Blueberry Fruit-Punch Orange

    Note that we don't need to use a list to add and replace values in our @KoolAidFlavors list:


    #!/usr/bin/perl

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

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

    print @KoolAidFlavors;

    print "\n\n";

    splice(@KoolAidFlavors, 1,2,('Pomegranate ','Blueberry '));

    print @KoolAidFlavors;

    print "\n\n";

    This gives us the same result as above:

      Grape Cherry Watermelon Fruit-Punch Orange

      Grape Pomegranate Blueberry Fruit-Punch Orange



     
     
    >>> 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
    Stay green...Green IT