Version 4 of split and join for nested lists

Updated 2011-02-20 06:25:32 by RLE

NEM 15Feb2004 - Just knocked up these replacements for split and join that 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...

NEM 6June2005 - Improved the implementation a bit.

Some usage examples:

 % set ret [multi::join {
   {Person {name "Neil Madden"} {age 24} {occupation "Research Associate"} }
   {Person {name "Foo Bar"} {age 99} {occupation "Example"} }
 } \n : = _]
 Person:name=Neil_Madden:age=24:occupation=Research_Associate
 Person:name=Foo_Bar:age=99:occupation=Example
 % join [multi::split $ret \n : = _] \n
 Person {name {Neil Madden}} {age 24} {occupation {Research Associate}}
 Person {name {Foo Bar}} {age 99} {occupation Example}

The Code:

Sorry about the poor 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] < 2} {
        eval [linsert $args 0 ::join $list]
    } else {
        set ret [list]
        set next [lrange $args 1 end]
        foreach item $list { lappend ret [eval [linsert $next 0 join $item]] }
        return [::join $ret [lindex $args 0]]
    }
 }
 proc ::multi::split {string args} {
    if {[llength $args] < 2} {
        eval [linsert $args 0 ::split $string]
    } else {
        set ret [list]
        set next [lrange $args 1 end]
        foreach item [::split $string [lindex $args 0]] {
            lappend ret [eval [linsert $next 0 split $item]]
        }
        return $ret
    }
 }