Perl
  Home arrow Perl arrow Page 4 - 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 - Splice...It Sounds Like a New Citrus Soda, But It's Not
    ( Page 4 of 4 )

    So far we have been confined to adding and removing elements from a list from the left and right hand sides. Which leaves us up nerd creek without a paddle if we need to remove a value in the middle. That isn't to say that you can't remove a value from the center; you can with some ridiculous code. Oh, you would like to see that, would you? So would my minimum word count:


    #!/usr/bin/perl

    @KoolAidFlavors;

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

    ','Orange ');

    print @KoolAidFlavors;

    print "\n\n";

    $someFlavor=shift(@KoolAidFlavors);

    $bestflavor=shift(@KoolAidFlavors);

    unshift(@KoolAidFlavors, $someFlavor);

    print @KoolAidFlavors;

    print "\n\n";

    print $bestflavor;

    This convoluted code is used to retrieve what I think is the best flavor from the @KoolAidFlavors list, which of course is Cherry. Since the functions we have covered so far only allow us to grab or add from the left or right hand side, we have to get tricky. We do this by creating two new variables and using shift to take out the first element, then use shift again to remove "Cherry," which now becomes the first element. We then use unshift to put "Grape" back into the @KoolAidFlavors list via the $someflavor variable that holds it. Finally, we print out the values of the modified list, and the $bestflavor variable.

    A little confusing right? Well fortunately we don't have to go so crazy to perform what should be, and is, a simple task. Here is how we accomplish the same madness with our friend the splice() function:


    #!/usr/bin/perl

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

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

    print @KoolAidFlavors;

    print "\n\n";

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

    print @KoolAidFlavors;

    print "\n\n";

    print $bestflavor;

    This will give us the same result as above:

      Grape Cherry Watermelon Fruit-Punch Orange

      Grape Watermelon Fruit-Punch Orange

      Cherry

    In the above example you will note that the splice() function has several arguments. They are (in order):


    • The list you wish to modify.

    • The element number where the function should start replacing (if you tell it 1, it will replace the element following that, or 2...if you type 3, element 4 will be the first removed, and so forth).

    • How many elements you want removed.

    • Which list of elements you would like to insert.

    Some of these arguments are optional (well, technically they all are) and they can be used in several different ways. But we will get to that in a bit.

    In our example above, we used the following line: $bestflavor=splice(@KoolAidFlavors, 1,1). You will note that this use of the function does not have 4 arguments. It has the list that we are going to modify, the position, then the number of elements to extract. The result of this line is that it looks in our @KoolAidFlavors list, sees "Grape" as the first value, and knows to start taking elements after that. It sees that it should only take one element, so it takes Cherry and stores it in the $bestflavor variable.

    Be aware that if we had told it to take 2 elements, it would have stored Cherry and Watermelon in our variable. And further, since there would be two values, we would have needed to make $bestflavor into @bestflavor.

    Well that is all the time we have for this article. In our next tutorial we will continue our discussion on lists in Perl, and perhaps get to hashes as well.

    Till then...



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