Charles Daniel
Charles Daniel

Reputation: 3

how to merge two lists in tcl8.4.x

I have lists like below as an output of some function,

puts $m 
{{bi 1/7 1/8}} {{uni 1/6}}

I need the above to be merged as {{bi 1/7 1/8} {uni 1/6}} as single list. I cant use concat as mine is 8.4.x. please help

Upvotes: 0

Views: 98

Answers (3)

rtx13
rtx13

Reputation: 2610

One of the following may meet your needs:

% puts $m
{{bi 1/7 1/8}} {{uni 1/6}}
% puts [list [join $m]]
{{bi 1/7 1/8} {uni 1/6}}
% puts [llength [list [join $m]]]
1
% 
% puts $m
{{bi 1/7 1/8}} {{uni 1/6}}
% puts [join $m]
{bi 1/7 1/8} {uni 1/6}
% puts [llength [join $m]]
2
% 

Upvotes: 0

Shawn
Shawn

Reputation: 52344

If you can install tcllib, the struct::list package claims to still support such an ancient version of tcl, and has a flatten command that will return your desired list.

Example tclsh repl session:

% package require struct::list
1.8.3
% set m {{{bi 1/7 1/8}} {{uni 1/6}}}
{{bi 1/7 1/8}} {{uni 1/6}}
% ::struct::list flatten $m
{bi 1/7 1/8} {uni 1/6}
% ::struct::list flatten -full $m
bi 1/7 1/8 uni 1/6               

Upvotes: 0

Donal Fellows
Donal Fellows

Reputation: 137567

I'm not quite sure how deep you want the list concatenation to go (note that concat actually predates Tcl 7.0 so you certainly have it!) but here's how to strip one level of list-ness off, using code written to work with 8.4:

proc concat_one_level {input_list} {
    set accum_list {}
    foreach item $input_list {
        eval [linsert $item 0 lappend accum_list]
        # From 8.5 onwards, we'd use this instead:
        #    lappend accum_list {*}$item
    }
    return $accum_list
}

Testing it interactively (in 8.5; I don't have an 8.4 installation any more):

% puts $m 
{{bi 1/7 1/8}} {{uni 1/6}}
% concat_one_level $m
{bi 1/7 1/8} {uni 1/6}
% concat_one_level [concat_one_level $m]
bi 1/7 1/8 uni 1/6

Upvotes: 1

Related Questions