Perl
  Home arrow Perl arrow Page 2 - 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

    PCmover - $15 Off with Coupon Code CJPH7Q

    Perl Lists: The Split() Function - Using Split() On a String
    (Page 2 of 4 )

    In our previous examples, we used a single character as our delimiter. But we aren't limited (pun intended) to that single character. Below are some examples using strings as a delimiter:


    #!/usr/bin/perl

    $Message = "Hidden Message: ----K----i----ll----yo----urse----lf\n\n";

    @DoIt = split(/----/,$Message);

    print @DoIt[0];

    print @DoIt[1];

    print @DoIt[2];

    print @DoIt[3]. " ";

    print @DoIt[4];

    print @DoIt[5];

    print @DoIt[6];

    Here our delimiter is “----”. The output of this nefarious code is:

      Hidden Message: Kill yourself

    You can also use a variable as your delimiter, like so:


    #!/usr/bin/perl

    $Message = "Hidden Message: ----K----i----ll----yo----urse----lf\n\n";

    $Del = "----";

    @DoIt = split(/$Del/,$Message);

    print @DoIt[0];

    print @DoIt[1];

    print @DoIt[2];

    print @DoIt[3]. " ";

    print @DoIt[4];

    print @DoIt[5];

    print @DoIt[6];

    This gives us the same result as before.

    If you want to get creative, you could also use a list. In this next example, we will create a string with several possible delimiters, and then use a list to switch back and forth between them:


    #!/usr/bin/perl

    $Data = "Here is some data: Apple,9~Beer,9~Cider\n\n";

    @Del = (',','9','~',);

    @Comma = split(/@Del[0]/,$Data);

    @Nine = split(/@Del[1]/,$Data);

    @Weird = split(/@Del[2]/,$Data);

    print @Comma[0];

    print @Comma[1];

    print @Comma[2];

    print @Comma[3];

    print @Nine[0];

    print @Nine[1];

    print @Nine[2];

    print @Nine[3];

    print @Weird[0];

    print @Weird[1];

    print @Weird[2];

    print @Weird[3];

    This results in the following:

      Here is some data: Apple9~Beer9~Cider

      Here is some data: Apple,~Beer,~Cider

      Here is some data: Apple,9Beer,9Cider

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