Here is one possible solution, if I understand your problem correctly:
set a {{123.4:xyz {p_q[5]}} {123.4:abc {r_s[6]}} mno}
puts $a
set b [list]
foreach item $a {
lappend b [lindex $item end]
}
puts ">$b<"
set b [concat {*}$b]
puts ">$b<"
Output:
{123.4:xyz {p_q[5]}} {123.4:abc {r_s[6]}} mno
>{p_q[5]} {r_s[6]} mno<
>p_q[5] r_s[6] mno<
Discussion
- The loop goes through each item in list
a
- For each item, pick out the last sub-item and append it to
b
- After the list exits,
b
is almost what you want (see the second line in the output)
- A call to
concat
will fix that to your liking.
Update
Donal Fellows pointed out that instead of using [concat {*}$b]
, I can use [join $b]
. This approach has a couple of advantages which I like:
- It works on older 8.4 systems, which does not support the
{*}
construct
- It is simpler to understand
However, the concat
approach offers an advantage: speed. Take a look at the following interactive session:
(Downloads2) 61 % set b {{p_q[5]} {r_s[6]} mno}
{p_q[5]} {r_s[6]} mno
(Downloads2) 62 % time {concat {*}$b} 10000
0.43452700000000005 microseconds per iteration
(Downloads2) 63 % time {join $b} 10000
0.603005 microseconds per iteration
The concat
speed is about 30% faster than join
in this case.