I have two list:
set lista {5 6}
set listb {8 9}
and the following code is used to loop the two lists:
foreach listaelement listbelement $lista $listb {
puts $listaelement &listbelement
}
how can we use foreach to achieve the output is:
first element from lista, first element from listb,
first element from lista, second element from listb,
second element from lista, first element from listb,
second element from lista, second element from listb,
5 8 5 9 6 8 6 9
Just nesting the loops and using -nonewline should give the output you desire:
NB: You need to use quotes in the
putsstatement to stop Tcl interpreting the first argument as a channel id.Tcl does allow you to reference multiple lists in a
foreachstatementHowever this gives
5 8 6 9as its output – not what you require.Edit: I’m pretty sure when I answered the question that the output was formatted as 1 line, if you actually want 1 line for each pair then you don’t need the
-nonewlineon theputsand the trailing spaces and finalputscan also go.