perlfunc
gods
<P>
split - split up a string using a regexp delimiter
<P>
<HR>
<P>
split
<FONT SIZE=-1>/PATTERN/,EXPR,LIMIT</FONT>
<P>
split
<FONT SIZE=-1>/PATTERN/,EXPR</FONT>
<P>
split
<FONT SIZE=-1>/PATTERN/</FONT>
<P>
split
<P>
<HR>
<P>
Splits a string into an array of strings, and returns it. By default, empty
leading fields are preserved, and empty trailing ones are deleted.
<P>
If not in list context, returns the number of fields found and splits into
the <CODE>@_</CODE> array. (In list context, you can force the split into <CODE>@_</CODE> by using <CODE>??</CODE> as the pattern delimiters, but it still returns the list value.) The use of
implicit split to <CODE>@_</CODE> is deprecated, however, because it clobbers your subroutine arguments.
<P>
If
<FONT SIZE=-1>EXPR</FONT> is omitted, splits the <CODE>$_</CODE> string. If
<FONT SIZE=-1>PATTERN</FONT> is also omitted, splits on whitespace (after skipping any leading whitespace). Anything matching
<FONT SIZE=-1>PATTERN</FONT> is taken to be a delimiter separating the fields. (Note that the delimiter may be longer than one character.)
<P>
If
<FONT SIZE=-1>LIMIT</FONT> is specified and positive, splits into no more than that many fields (though it may split into fewer). If
<FONT SIZE=-1>LIMIT</FONT> is unspecified or zero, trailing null fields are stripped (which potential users of
[perlfunc:pop|pop()] would do well to remember). If
<FONT SIZE=-1>LIMIT</FONT> is negative, it is treated as if an arbitrarily large
<FONT SIZE=-1>LIMIT</FONT> had been specified.
<P>
<FONT SIZE=-1>A</FONT> pattern matching the null string (not to be confused
with a null pattern <CODE>//</CODE>, which is just one member of the set of patterns matching a null string) will split the value of
<FONT SIZE=-1>EXPR</FONT> into separate characters at each point it matches that way. For example:
<P>
<PRE> print join(':', split(/ */, 'hi there'));
</PRE>
<P>
produces the output 'h:i:t:h:e:r:e'.
<P>
The
<FONT SIZE=-1>LIMIT</FONT> parameter can be used to split a line partially
<P>
<PRE> ($login, $passwd, $remainder) = split(/:/, $_, 3);
</PRE>
<P>
When assigning to a list, if
<FONT SIZE=-1>LIMIT</FONT> is omitted, Perl supplies a
<FONT SIZE=-1>LIMIT</FONT> one larger than the number of variables in the list, to avoid unnecessary work. For the list above
<FONT SIZE=-1>LIMIT</FONT> would have been 4 by default. In time critical applications it behooves you not to split into more fields than you really need.
<P>
If the
<FONT SIZE=-1>PATTERN</FONT> contains parentheses, additional array
elements are created from each matching substring in the delimiter.
<P>
<PRE> split(/([,-])/, "1-10,20", 3);
</PRE>
<P>
produces the list value
<P>
<PRE> (1, '-', 10, ',', 20)
</PRE>
<P>
If you had the entire header of a normal Unix email message in <CODE>$header</CODE>, you could split it up into fields and their values this way:
<P>
<PRE> $header =~ s/\n\s+/ /g; # fix continuation lines
%hdrs = (UNIX_FROM => split /^(\S*?):\s*/m, $header);
</PRE>
<P>
The pattern <CODE>/PATTERN/</CODE> may be replaced with an expression to specify patterns that vary at
runtime. (To do runtime compilation only once, use <CODE>/$variable/o</CODE>.)
<P>
As a special case, specifying a
<FONT SIZE=-1>PATTERN</FONT> of space (<CODE>' '</CODE>) will split on white space just as [perlfunc:split|split()] with no arguments does. Thus, [perlfunc:split|split(' ')] can be used to emulate <STRONG>awk</STRONG>'s default behavior, whereas [perlfunc:split|split(/ /)]
will give you as many null initial fields as there are leading spaces.
<FONT SIZE=-1>A</FONT> [perlfunc:split|split()] on [%linkNodeTitle "perlman:perlop|/\s+/", $NODE, "/\s+/",{anchor=>"item__s_"};%] is like a [perlfunc:split|split(' ')] except that any leading whitespace produces a null first field.
<FONT SIZE=-1>A</FONT> [perlfunc:split|split()] with no arguments really does a [perlfunc:split|split(' ', $_)] internally.
<P>
Example:
<P>
<PRE> open(PASSWD, '/etc/passwd');
while (<PASSWD>) {
($login, $passwd, $uid, $gid,
$gcos, $home, $shell) = split(/:/);
#...
}
</PRE>
<P>
(Note that <CODE>$shell</CODE> above will still have a newline on it. See [perlfunc:chop|chop],
[perlfunc:chomp|chomp], and [perlfunc:join|join].)
<HR>