Version 1 of split and join for nested lists

Updated 2004-02-15 16:24:22

NEM 15Feb2004 - Just knocked up these replacements for split and join which allow nested lists to be dealt with. The implementations are recursive, and so not terribly efficient, but they could have uses. Might be a possibility for a TIP...

Some usage examples:

 multi::join {{a b} {c d}} \n =

gives

 a=b
 c=d

while, for split:

 multi::split {a=b
 c=d} \n =

gives

 {{a b} {c d}}

The Code:

Sorry about the crap choice of namespace, but I was doing this in a hurry. If you think of a better name, change it...

 package provide multi 1.0
 namespace eval multi { }
 proc multi::join {list args} {
     if {[llength $args] == 0} {
         return $list
     } else {
         set head [lindex $args 0]
         set rest [lrange $args 1 end]
     }
     set ret ""
     if {[llength $list] > 1} {
         foreach item [lrange $list 0 end-1] {
             append ret [eval [linsert $rest 0 join $item]]$head
         }
     }
     append ret [eval [linsert $rest 0 join [lindex $list end]]]
     return $ret
 }

 proc multi::split {string args} {
     if {[llength $args] == 0} {
         return $string
     } else {
         set head [lindex $args 0]
         set rest [lrange $args 1 end]
     }
     set ret [list]
     foreach item [::split $string $head] {
         lappend ret [eval [linsert $rest 0 split $item]]
     }
     return $ret
 }