Perl in a Nutshell

Perl in a NutshellSearch this book
Previous: Reference: spliceChapter 5
Function Reference
Next: Reference: sprintf
 

split

split /pattern/, string, [limit]

Scans a string for delimiters that match pattern, and splits the string into a list of substrings, returning the resulting list value in list context, or the count of substrings in scalar context. The delimiters are determined by repeated pattern matching, using the regular expression given in pattern, so the delimiters may be of any size, and need not be the same string on every match. If the pattern doesn't match at all, split returns the original string as a single substring. If it matches once, you get two substrings, and so on.

If limit is specified and is not negative, the function splits into no more than that many fields. If limit is negative, it is treated as if an arbitrarily large limit has been specified. If limit is omitted, trailing null fields are stripped from the result (which potential users of pop would do well to remember). If string is omitted, the function splits the $_ string. If pattern is also omitted, the function splits on whitespace, /\s+/, after skipping any leading whitespace.

If the pattern contains parentheses, then the substring matched by each pair of parentheses is included in the resulting list, interspersed with the fields that are ordinarily returned. Here's a simple case:

split /([-,])/, "1-10,20";
that produces the list value:
(1, '-', 10, ',', 20)


Previous: Reference: splicePerl in a NutshellNext: Reference: sprintf
Reference: spliceBook IndexReference: sprintf

Library Navigation Links

Copyright © 2001 O'Reilly & Associates. All rights reserved.