Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

List manipulation 1

Status
Not open for further replies.

fhutt

Programmer
Jul 7, 2006
79
AU
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
 
Hi

Huh ? That works for me.
Code:
[b]set[/b] var1 [teal]{[/teal]one two[teal]}[/teal]
[b]set[/b] new [teal][[/teal][b]lindex[/b] [navy]$var1[/navy] [purple]1[/purple][teal]][/teal]
[b]puts[/b] [navy]$new[/navy]
Code:
[blue]master #[/blue] tclsh fhutt.tcl
two

[blue]master #[/blue] tclsh <<< 'puts $tcl_version'
8.5

Feherke.
 
No.
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
 
Hi

Ah, got it. Glad to see you solved it.

( My suggestion was [tt]set new [teal][[/teal]lindex [teal][[/teal]lindex [navy]$var1[/navy] [purple]0[/purple][teal]][/teal] [purple]1[/purple][teal]][/teal][/tt]. Uglier, but faster. )


Feherke.
 
there are many roads to...the solution:

set var1 "\{one two\}"
set new [lindex $var1 0 1]
Now new will contain two
 
Thank you, great ideas and reading the manual!
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top