List manipulation
List manipulation
(OP)
Hello
I have continuing problem removing braces.
Here is simple example.
I have a variable 'var1' with contents {one two}
What is the easiest way to extract 'two' from the variable. I tried
set new [lindex $var1 1]
but of course it is a blank since {one two} is a single element.
How do I extract the 'two' the simplest way?
Thanks
I have continuing problem removing braces.
Here is simple example.
I have a variable 'var1' with contents {one two}
What is the easiest way to extract 'two' from the variable. I tried
set new [lindex $var1 1]
but of course it is a blank since {one two} is a single element.
How do I extract the 'two' the simplest way?
Thanks
RE: List manipulation
Huh ? That works for me.
CODE --> fhutt.tcl
set new [lindex $var1 1]
puts $new
CODE --> command line
two
master # tclsh <<< 'puts $tcl_version'
8.5
Feherke.
http://feherke.github.com/
RE: List manipulation
When I meant that var1 has contents {one two}, I didn't mean:
set var1 {one two}
I meant:
set var1 "\{one two\}"
With set var1 {one two}, var1 actually contains one two, not {one two}
I think I found the answer though.
set var1 "\{one two\}"
set new [lindex [join $var1] 1]
Now new will contain two
Thanks
RE: List manipulation
Ah, got it. Glad to see you solved it.
( My suggestion was set new [lindex [lindex $var1 0] 1]. Uglier, but faster. )
Feherke.
http://feherke.github.com/
RE: List manipulation
set var1 "\{one two\}"
set new [lindex $var1 0 1]
Now new will contain two
RE: List manipulation
Yay ! I think it is time to re-read the whole Tcl documentation.
Good point thacoda.
Feherke.
http://feherke.github.com/
RE: List manipulation
They all work but needs to be used carefully.
The all work with
set var1 "\{one two\}"
but only the join method works with
set var1 {one two}
Sometimes the format cannot be predicted.
They also all work with extracting index 0 instead of index 1:
eg.
set new [lindex $var1 0 0]
set new [lindex [lindex $var1 0] 0]
set new [lindex [join $var1] 0]
Frank