PERLFUNC: split - split up a string using a regexp delimiter 
Author Message
 PERLFUNC: split - split up a string using a regexp delimiter

(This excerpt is from the perlfunc manpage, part of the standard set of
documentation included with every valid Perl distribution--like the one on
your own system.  See also http://www.*-*-*.com/
or http://www.*-*-*.com/
if your negligent system adminstrator has been remiss in his duties.)

    split - split up a string using a regexp delimiter


    split /PATTERN/,EXPR

    split /PATTERN/


    Splits a string into a list of strings and returns that list. By
    default, empty leading fields are preserved, and empty trailing
    ones are deleted.

    If not in list context, returns the number of fields found and

    deprecated, however, because it clobbers your subroutine arguments.

    If EXPR is omitted, splits the `$_' string. If PATTERN is also
    omitted, splits on whitespace (after skipping any leading
    whitespace). Anything matching PATTERN is taken to be a delimiter
    separating the fields. (Note that the delimiter may be longer than
    one character.)

    If LIMIT is specified and positive, splits into no more than that
    many fields (though it may split into fewer). If LIMIT is
    unspecified or zero, trailing null fields are stripped (which
    potential users of `pop' would do well to remember). If LIMIT is
    negative, it is treated as if an arbitrarily large LIMIT had been

    A pattern matching the null string (not to be confused with a null
    pattern `//', which is just one member of the set of patterns
    matching a null string) will split the value of EXPR into separate
    characters at each point it matches that way. For example:

        print join(':', split(/ */, 'hi there'));

    produces the output 'h:i:t:h:e:r:e'.

    The LIMIT parameter can be used to split a line partially

        ($login, $passwd, $remainder) = split(/:/, $_, 3);

    When assigning to a list, if LIMIT is omitted, Perl supplies a
    LIMIT one larger than the number of variables in the list, to avoid
    unnecessary work. For the list above LIMIT would have been 4 by
    default. In time critical applications it behooves you not to split
    into more fields than you really need.

    If the PATTERN contains parentheses, additional list elements are
    created from each matching substring in the delimiter.

        split(/([,-])/, "1-10,20", 3);

    produces the list value

        (1, '-', 10, ',', 20)

    If you had the entire header of a normal Unix email message in
    $header, you could split it up into fields and their values this

        $header =~ s/\n\s+/ /g;  # fix continuation lines
        %hdrs   =  (UNIX_FROM => split /^(\S*?):\s*/m, $header);

    The pattern `/PATTERN/' may be replaced with an expression to
    specify patterns that vary at runtime. (To do runtime compilation
    only once, use `/$variable/o'.)

    As a special case, specifying a PATTERN of space (`' '') will split
    on white space just as `split' with no arguments does. Thus,
    `split(' ')' can be used to emulate awk's default behavior, whereas
    `split(/ /)' will give you as many null initial fields as there are
    leading spaces. A `split' on `/\s+/' is like a `split(' ')' except
    that any leading whitespace produces a null first field. A `split'
    with no arguments really does a `split(' ', $_)' internally.


        open(PASSWD, '/etc/passwd');
        while (<PASSWD>) {
            ($login, $passwd, $uid, $gid,
             $gcos, $home, $shell) = split(/:/);

    (Note that $shell above will still have a newline on it. See the
    "chop" entry in the perlfunc manpage, the "chomp" entry in the
    perlfunc manpage, and the "join" entry in the perlfunc manpage.)

Churchill's Commentary on Man: Man will occasionally stumble over the
truth, but most of the time he will pick himself up and continue on.

Wed, 07 Nov 2001 03:00:00 GMT  
 [ 1 post ] 

 Relevant Pages 

1. PERLFUNC: split - split up a string using a regexp delimiter

2. RegExp for the following string, w/o using split

3. - split strings at arbitrary delimiters

4. Using a period as a delimiter in the split() function

5. Using Different Delimiters in Split

6. Using a period as a delimiter in the split() function

7. Help: bol regexp in split string

8. Using split to return a @ without splitting on spaces

9. splitting with embedded delimiters

10. SPLIT Problem - Embedded Delimiter

11. splitting but maintaining delimiter?

12. returning split() delimiters...


Powered by phpBB® Forum Software