home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 5 / hacker05 / 05_HACK_05.ISO / programacao / freewrap / TCLLIBsampleApp.exe / sample / tcllib / tcllib1.0 / textutil / split.tcl < prev    next >
Encoding:
Text File  |  2001-08-17  |  1.7 KB  |  53 lines

  1. namespace eval ::textutil {
  2.  
  3.     namespace eval split {
  4.  
  5.     namespace export splitx
  6.  
  7.     # This will be redefined later. We need it just to let
  8.     # a chance for the next import subcommand to work
  9.     #
  10.     proc splitx [list str [list regexp "\[\t \r\n\]+"]] {}
  11.     }
  12.  
  13.     namespace import -force split::splitx
  14.     namespace export splitx
  15.  
  16. }
  17.  
  18. ########################################################################
  19. # This one was written by Bob Techentin (RWT in Tcl'ers Wiki):
  20. # http://www.techentin.net
  21. # mailto:techentin.robert@mayo.edu
  22. #
  23. # Later, he send me an email stated that I can use it anywhere, because
  24. # no copyright was added, so the code is defacto in the public domain.
  25. #
  26. # You can found it in the Tcl'ers Wiki here:
  27. # http://mini.net/cgi-bin/wikit/460.html
  28. #
  29. # Bob wrote:
  30. # If you need to split string into list using some more complicated rule
  31. # than builtin split command allows, use following function. It mimics
  32. # Perl split operator which allows regexp as element separator, but,
  33. # like builtin split, it expects string to split as first arg and regexp
  34. # as second (optional) By default, it splits by any amount of whitespace. 
  35. # Note that if you add parenthesis into regexp, parenthesed part of separator
  36. # would be added into list as additional element. Just like in Perl. -- cary 
  37. #
  38.  
  39. proc ::textutil::split::splitx [list str [list regexp "\[\t \r\n\]+"]] {
  40.     set list  {}
  41.     while {[regexp -indices -- $regexp $str match submatch]} {
  42.     lappend list [string range $str 0 [expr {[lindex $match 0] -1}]]
  43.     if {[lindex $submatch 0]>=0} {
  44.         lappend list [string range $str [lindex $submatch 0]\
  45.                   [lindex $submatch 1]]
  46.     }
  47.     set str [string range $str [expr {[lindex $match 1]+1}] end]
  48.     }
  49.     lappend list $str
  50.     return $list
  51. }
  52.  
  53.