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

Perl Lists: The Split() Function
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    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:
      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: 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
     

       

    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 5 Hosted by Hostway
    Stay green...Green IT