1. Home
  2. Computing & Technology
  3. Perl

Perl array split() function - Quick Tutorial
How to use the split() function

From , former About.com Guide

@LIST = split(/PATTERN/, STRING, LIMIT);
Perl's split() function is used to break a string into an array on a specific pattern. The PATTERN is a regular expression that can be as simple as a single character. By default the STRING is split on every instance of the PATTERN, but you can LIMIT it to a specific number of instances.
$myNames = "Jacob,Michael,Joshua,Matthew,Ethan,Andrew";
@nameList = split(/,/, $myNames);
In the above example, the split function takes a plain string of names, separated by commas. It then breaks the string apart on each instance of the comma, putting the results into the array @nameList. The value of @nameList would then be:
@myNames = ('Jacob', 'Michael', 'Joshua', 'Matthew', 'Ethan', 'Andrew');
By using the LIMIT option, you can split the array into a specific size. For example:
$myNames = "Jacob,Michael,Joshua,Matthew,Ethan,Andrew";
@nameList = split(/,/, $myNames, 3);
This would split the array into 3 chunks, so the value of @nameList would then be:
@myNames = ('Jacob', 'Michael', 'Joshua,Matthew,Ethan,Andrew');
More Perl Quick Tips
Explore Perl
About.com Special Features

The Best Web Trends of the Decade

A look back at the best innovations, ideas and technologies over the last 10 years, More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Perl
  4. Programming Perl
  5. Perl split function reference - learn how to use Perl's split() function in this quick tutorial.

©2009 About.com, a part of The New York Times Company.

All rights reserved.