Perl
  Home arrow Perl arrow Page 3 - Perl Lists: The Split() Function
FaxWave - Free Trial.
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: The Split() Function
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-04-07

    Table of Contents:
  • Perl Lists: The Split() Function
  • Using Split() On a String
  • Limiting the Amount of Splits
  • Assigning a List to Another List

  • 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Perl Lists: The Split() Function - Limiting the Amount of Splits
    (Page 3 of 4 )

    Sometimes you might not wish to split the entire string. Say you just want the first two delimited results, and not all of them. Here is how you would get those results:


    #!/usr/bin/perl

    $Some = "Hamburger--Fries--Tofu--Banana";

    @Yum = split(/--/,$Some,3);

    print @Yum[0] . "\n";

    print @Yum[1] . "\n";

    print @Yum[2] . "\n";

    In this example, the result is:

      Hamburger

      Fries

      Tofu--Banana

    What happens here is that the function splits the first and second words and puts them into the first and second ([0] and [1]) positions in the @Yum array. It then stops seeking the delimiter and places whatever is left over into the third position of our array. That is why Tofu and Banana are still together, and indeed, even have their delimiter as well.

    We can also use this method to assign values to a group of variables. Let's say we have a string that lists several types of meat and we want each type of meat in its own variable. Here is how we would achieve this:


    #!/usr/bin/perl

    $Some = "Hamburger Pork Chicken Fish";

    ($Meat, $Pork, $Poultry, $Fish) =

    split(/t/,$Some,4);

    print $Meat . "\n";

    print $Pork . "\n";

    print $Poultry . "\n";

    print $Fish . "\n";

    The result is:

      Hamburger

      Pork

      Chicken

      Fish

    You may have noticed something bizarre in the code where we usually place our delimiter. There is still a delimiter here; we are simply delimiting by the special character t, or tab.

    You can also use split() on text files, but we will save that for a future tutorial.

    More Perl Articles
    More By James Payne


       · Welcome to another article in our series on Perl Lists and Hashes. In this episode...
     

       

    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

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




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