9.7. The split Operator
Another operator that uses regular expressions is split, which breaks up a string according to a separator. This is useful for tab-separated data, or colon-separated, whitespace-separated, or anything-separated data, really. So long as you can specify the separator with a regular expression (and generally, it's a simple regular expression), you can use split. It looks like this:
@fields = split /separator/, $string;
The split operator drags the pattern through a string and returns a list of fields (substrings) that were separated by the separators. Whenever the pattern matches, that's the end of one field and the start of the next. So, anything that matches the pattern will never show up in the returned fields. Here's a typical split pattern, splitting on colons:
@fields = split /:/, "abc:def:g:h"; # gives ("abc", "def", "g", "h")
You could even have an empty field, if there were two delimiters together:
@fields = split /:/, "abc:def::g:h"; # gives ("abc", "def", "", "g", "h")
Here's a rule that seems odd at first, but it rarely causes problems: Leading empty fields are always returned, but trailing empty fields are discarded:
@fields = split /:/, ":::a:b:c:::"; # gives ("", "", "", "a", "b", "c")
my $some_input = "This is a \t test.\n"; my @args = split /\s+/, $some_input; # ("This", "is", "a", "test.")
The default for split is to break up $_ on whitespace:
my @fields = split; # like split /\s+/, $_;
This is almost the same as using /\s+/ as the pattern, except that a leading empty field is suppressed -- so, if the line starts with whitespace, you won't see an empty field at the start of the list. (If you'd like to get the same behavior when splitting another string on whitespace, just use a single space in place of the pattern: split ' ', $other_string. Using a space instead of the pattern is a special kind of split.)
Generally, the patterns used for split are as simple as the ones you see here. But if the pattern becomes more complex, be sure to avoid using memory parentheses in the pattern; see the perlfunc manpage for more information.
Copyright © 2002 O'Reilly & Associates. All rights reserved.