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