Perl
  Home arrow Perl arrow Page 4 - 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 - Adding Values to a List with Splice()
    ( Page 4 of 5 )

    So far we have seen how to remove items from lists and replace items using the splice() function. In this section we will learn to simply add values. It works in a similar manner, only you change the number for the elements to remove to 0, like so:


    #!/usr/bin/perl

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

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

    print @KoolAidFlavors;

    print "\n\n";

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

    print @KoolAidFlavors;

    print "\n\n";

    Here, instead of inserting and replacing the values Cherry and Watermelon with Pomegranate and Blueberry, we will append them to the table. In the line: splice(KoolAidFlavors,1,0,('Pomegranate ','Blueberry ')); we tell the program to insert the two values after the first element. Remember that the 0 tells the program not to replace any elements. If we had written 2,0 then the values would have been added after the second element, and so forth.

    The result:

      Grape Cherry Watermelon Fruit-Punch Orange

      Grape Pomegranate Blueberry Cherry Watermelon Fruit-Punch Orange

    And just as we can remove values in our @KoolAidFlavors list by using another list, so too, can we add them:


    #!/usr/bin/perl

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

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

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

    print @KoolAidFlavors;

    print "\n\n";

    splice(@KoolAidFlavors, 1,0,(@NewFlavors));

    print @KoolAidFlavors;

    print "\n\n";

    The result:

      Grape Cherry Watermelon Fruit-Punch Orange

      Grape Pomegranate Blueberry Cherry Watermelon Fruit-Punch Orange

    It looks the same as before. Which way is best really depends on the situation and your own personal preferences.



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