Perl
  Home arrow Perl arrow Page 3 - Perl Lists: More on Manipulation
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? 
Google.com  
PERL

Perl Lists: More on Manipulation
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2008-03-24


    Table of Contents:
  • Perl Lists: More on Manipulation
  • Pop() Goes Your Data
  • Unshift My Heart
  • Splice...It Sounds Like a New Citrus Soda, But It's Not

  • 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 on Manipulation - Unshift My Heart
    ( Page 3 of 4 )

    The unshift() function is similar to push except that it adds an item to the front or left side of your list. For this example, let's start with a blank list and fill it with data (you can do the same using push):


    #!/usr/bin/perl

    @KoolAidFlavors;

    unshift(@KoolAidFlavors, 'Grape ','Cherry ','Watermelon ','Fruit-Punch

    ','Orange ');

    print @KoolAidFlavors;

    The result?

      Grape Cherry Watermelon Fruit-Punch Orange

    On the Night Shift

    The shift() function works like the pop() function except it removes the element from the left side of the list. Want to see the code? Too bad:


    #!/usr/bin/perl

    @KoolAidFlavors;

    unshift(@KoolAidFlavors, 'Grape ','Cherry ','Watermelon ','Fruit-Punch

    ','Orange ');

    print @KoolAidFlavors;

    print "\n\n";

    $grossFlavors=shift(@KoolAidFlavors);

    print @KoolAidFlavors;

    print "\n\n";

    print $grossFlavors;

    Again, we create the @KoolAidFlavors as a null list, then add values to it using the unshift() function. Next we print out the value, then create a new variable named $grossFlavors, which will hold the gross flavors from the list. Of course, the gross flavor must also be the first element for this to work. We use shift() to grab the first element in the list, then print out both the updated list and the variable, resulting in:

      Grape Cherry Watermelon Fruit-Punch Orange

      Cherry Watermelon Fruit-Punch Orange

      Grape

    Tada!



     
     
    >>> 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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek