Perl
  Home arrow Perl arrow Page 3 - Perl Lists: More on Manipulation
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 on Manipulation
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 3
    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:
      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

    Ziff Davis Enterprise Virtual Tradeshows: Hot Topics, Cutting Edge Technology, Real-time Interaction with IT Professionals. Learn more at ziffdavisvts.com

    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


       · Thanks for stopping by to read another installment on my series about Perl Lists and...
     

       

    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




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