Perl
  Home arrow Perl arrow Page 3 - 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? 
Google.com  
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 - 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
     

       

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