Posted to tcl by lmcvoy at Sun Nov 25 21:32:38 GMT 2007view raw

  1. split /PATTERN/,EXPR,LIMIT
  2. split /PATTERN/,EXPR
  3. split /PATTERN/
  4. split Splits the string EXPR into a list of strings and returns that
  5. list. By default, empty leading fields are preserved, and
  6. empty trailing ones are deleted. (If all fields are empty,
  7. they are considered to be trailing.)
  8.  
  9. In scalar context, returns the number of fields found and
  10. splits into the @_ array. Use of split in scalar context is
  11. deprecated, however, because it clobbers your subroutine argu-
  12. ments.
  13.  
  14. If EXPR is omitted, splits the $_ string. If PATTERN is also
  15. omitted, splits on whitespace (after skipping any leading
  16. whitespace). Anything matching PATTERN is taken to be a delim-
  17. iter separating the fields. (Note that the delimiter may be
  18. longer than one character.)
  19.  
  20. If LIMIT is specified and positive, it represents the maximum
  21. number of fields the EXPR will be split into, though the actual
  22. number of fields returned depends on the number of times PAT-
  23. TERN matches within EXPR. If LIMIT is unspecified or zero,
  24. trailing null fields are stripped (which potential users of
  25. "pop" would do well to remember). If LIMIT is negative, it is
  26. treated as if an arbitrarily large LIMIT had been specified.
  27. Note that splitting an EXPR that evaluates to the empty string
  28. always returns the empty list, regardless of the LIMIT speci-
  29. fied.
  30.  
  31. A pattern matching the null string (not to be confused with a
  32. null pattern "//", which is just one member of the set of pat-
  33. terns matching a null string) will split the value of EXPR into
  34. separate characters at each point it matches that way. For
  35. example:
  36.  
  37. print join(':', split(/ */, 'hi there'));
  38.  
  39. produces the output 'h:i:t:h:e:r:e'.
  40.  
  41. As a special case for "split", using the empty pattern "//"
  42. specifically matches only the null string, and is not be con-
  43. fused with the regular use of "//" to mean "the last successful
  44. pattern match". So, for "split", the following:
  45.  
  46. print join(':', split(//, 'hi there'));
  47.  
  48. produces the output 'h:i: :t:h:e:r:e'.
  49.  
  50. Empty leading (or trailing) fields are produced when there are
  51. positive width matches at the beginning (or end) of the string;
  52. a zero-width match at the beginning (or end) of the string does
  53. not produce an empty field. For example:
  54.  
  55. print join(':', split(/(?=\w)/, 'hi there!'));
  56.  
  57. produces the output 'h:i :t:h:e:r:e!'.
  58.  
  59. The LIMIT parameter can be used to split a line partially
  60.  
  61. ($login, $passwd, $remainder) = split(/:/, $_, 3);
  62.  
  63. When assigning to a list, if LIMIT is omitted, or zero, Perl
  64. supplies a LIMIT one larger than the number of variables in the
  65. list, to avoid unnecessary work. For the list above LIMIT
  66. would have been 4 by default. In time critical applications it
  67. behooves you not to split into more fields than you really
  68. need.
  69.  
  70. If the PATTERN contains parentheses, additional list elements
  71. are created from each matching substring in the delimiter.
  72.  
  73. split(/([,-])/, "1-10,20", 3);
  74.  
  75. produces the list value
  76.  
  77. (1, '-', 10, ',', 20)
  78.  
  79. If you had the entire header of a normal Unix email message in
  80. $header, you could split it up into fields and their values
  81. this way:
  82.  
  83. $header =~ s/\n\s+/ /g; # fix continuation lines
  84. %hdrs = (UNIX_FROM => split /^(\S*?):\s*/m, $header);
  85.  
  86. The pattern "/PATTERN/" may be replaced with an expression to
  87. specify patterns that vary at runtime. (To do runtime compila-
  88. tion only once, use "/$variable/o".)
  89.  
  90. As a special case, specifying a PATTERN of space (' ') will
  91. split on white space just as "split" with no arguments does.
  92. Thus, "split(' ')" can be used to emulate awk's default behav-
  93. ior, whereas "split(/ /)" will give you as many null initial
  94. fields as there are leading spaces. A "split" on "/\s+/" is
  95. like a "split(' ')" except that any leading whitespace produces
  96. a null first field. A "split" with no arguments really does a
  97. "split(' ', $_)" internally.
  98.  
  99. A PATTERN of "/^/" is treated as if it were "/^/m", since it
  100. isn't much use otherwise.
  101.  
  102. Example:
  103.  
  104. open(PASSWD, '/etc/passwd');
  105. while (<PASSWD>) {
  106. chomp;
  107. ($login, $passwd, $uid, $gid,
  108. $gcos, $home, $shell) = split(/:/);
  109. #...
  110. }
  111.  
  112. As with regular pattern matching, any capturing parentheses
  113. that are not matched in a "split()" will be set to "undef" when
  114. returned:
  115.  
  116. @fields = split /(A)|B/, "1A2B3";
  117. # @fields is (1, 'A', 2, undef, 3)
  118.