Perl
  Home arrow Perl arrow Perl Lists: The Split() Function
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

    TestComplete™ automates software testing for a fraction of what the big guys charge. Easy functional and load testing for all Windows, .NET, Java and Web apps. Download a free trial now.

    Perl Lists: The Split() Function
    (Page 1 of 4 )

    In this fourth part of our series on Lists, we will start off with the split() function and hopefully end by covering hashes. In our last article, we covered the splice() function, which we used to add, remove, and replace elements in a list. We then used it to create variables and arrays. We also worked with some operators to repeat a list and to create sequential lists.

    Split()ting Hairs

    The split() function has many uses in Perl. Its main purpose is to take a string and break it apart, returning a list of strings. In our first example, we will take a sentence and break each word into a list. Here it is in code:


    #!/usr/bin/perl

    $Trees = "Why aren't there any fat trees? All they do is eat all day

    and sit around.\n\n";

    @Pieces = split(/ /, $Trees);

    print $Trees;

    print @Pieces;

    This code takes each word in the $Trees string and puts each one as an element in the @Pieces list. The split() function above has the following as its argument: (/ /, $Trees). The first part, “/ /” is two forward slashes with a space between them. Whatever you place in between the forward slashes is used as a delimiter. So in this instance, split() looks at the $Trees variable, finds the first word, sees a space, then adds that word to the @Pieces first element. Next it sees the next word, finds a space after it, and adds that word as the second element, and so forth. The print out of the above text is:

      Why aren't there any fat trees? All they do is eat all day and sit around.

      Whyaren'tthereanyfattrees?Alltheydoiseastalldayandsitaround.

    To better understand what is going on, let's modify the code a little bit:


    #!/usr/bin/perl

    $Trees = "Why aren't there any fat trees. All they do is eat all day

    and sit around.\n\n";

    @Pieces = split(/ /, $Trees);

    print $Trees;

    print @Pieces[0] . " ";

    print @Pieces[1] . " ";

    print @Pieces[2] . " ";

    print @Pieces[3] . " ";

    print @Pieces[4] . " ";

    In the above example, the code is modified a bit so that we print out some of the individual elements in the @Pieces array. You will note that I appended a space after each element, just to make it more legible.

    The end result is:

      Why aren't there any fat trees? All they do is eat all day and sit around.

      Why aren't there any fat

    We can use anything as our delimiter when we use the split() function. In this next example, we use a comma, though this should be avoided as it can lead to mistakes if you have commas in your strings that you do not wish to have parsed and you forget about them (we'll discuss how to deal with this issue later):


    #!/usr/bin/perl

    $Numlist = "Here are some numbers: 1,2,3,4,5\n\n";

    @Numbers = split(/,/,$Numlist);

    print @Numbers[0] . " ";

    print @Numbers[1] . " ";

    print @Numbers[2] . " ";

    print @Numbers[3] . " ";

    This program results in:

      Here are some numbers: 1 2 3 4

    In the above example, what do you think the value of @Number[0] is? If you think it is “1”, think again. Try this code:


    #!/usr/bin/perl

    $Numlist = "Here are some numbers: 1,2,3,4,5\n\n";

    @Numbers = split(/,/,$Numlist);

    print @Numbers[0];

    The result is:

    Here are some numbers: 1

    This is because the program searches for the delimiter and then takes the value. So everything preceding the delimiter is taken, hence our result.

    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: 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
    - Perl: Releasing Your Inner Textuality

     
    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 1 hosted by Hostway