Perl
  Home arrow Perl arrow Page 4 - Perl: Sailing the List(less) Seas
Dev Shed Forums 
Administration  
AJAX  
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 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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: Sailing the List(less) Seas
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-03-17

    Table of Contents:
  • Perl: Sailing the List(less) Seas
  • Printing Specific Elements
  • Slices
  • Replacing an Element Using a Slice OR Slice and Dice

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl: Sailing the List(less) Seas - Replacing an Element Using a Slice OR Slice and Dice


    (Page 4 of 4 )

    One method for replacing an element with another element is to use a slice, as demonstrated in the following example. Let's say that Nitro got mad because his mullet got pulled one too many times and he decided to quit. The crew at Gladiators interviewed a bunch of muscles and decided to go with a five foot four, 300 pound guy with a napoleon complex named Glutious-Minimus. Now they would have to add him to their list and remove Nitro, like so:


    #!/usr/bin/perl

    @gladiators=('Nitro ', 'Blaze ', 'CountFistula ','TheNutcracker ');

    $gladiators[0] = "Glutious-Minimus ";

    print @gladiators;

    This code does what we set out for it to do: remove Nitro and insert Glutious in his position. If we had said $gladiators[1], then it would have replaced Blaze, and so forth.

    Here is the result:

      Glutious-Minimus Blaze CountFistula TheNutcracker

    But what if Nitro didn't quit and the Gladiators just wanted to expand, and hired on our wee friend Glutious? We can add him using the same method; just append him to the end of the list, like so:


    #!/usr/bin/perl

    @gladiators=('Nitro ', 'Blaze ', 'CountFistula ','TheNutcracker ');

    $gladiators[4] = "Glutious-Minimus ";

    print @gladiators;

    Now we have the result:

      Nitro Blaze CountFistula TheNutcracker Glutious-Minimus

    And finally, if we want to leave space for another gladiator and don't want to append them to the end, we don't have to assign Glutious to position four at all. We could put him at five, six, or whatever position we like. Here, we will put him in position five, leaving four open:


    #!/usr/bin/perl

    @gladiators=('Nitro ', 'Blaze ', 'CountFistula ','TheNutcracker ');

    $gladiators[5] = "Glutious-Minimus ";

    print @gladiators;

    When you print this out, you cannot tell that four is open. But what if we use our element counter from before?


    #!/usr/bin/perl

    @gladiators=('Nitro ', 'Blaze ', 'CountFistula ','TheNutcracker ');

    $gladiators[5] = "Glutious-Minimus ";

    print $#gladiators;

    As you can see, this will print out 5, which isn't really the amount of elements in the array. Remember, when using this method it simply gives you the last numbered element, not the real number of elements in the array.

    Well that's it for this article. In the next one we will cover some more ways to work with lists and likely delve into hashes as well. So drop by often.

    Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Thank you for stopping by to read my article on Perl Lists. In this article we...
     

       

    PERL ARTICLES

    - Perl: More on Lists and Hashes
    - Perl: Dimensional Lists
    - 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





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