INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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
 

RE: List manipulation

Hi

Huh ? That works for me.

CODE --> fhutt.tcl

set var1 {one two}
set new [lindex $var1 1]
puts $new

CODE --> command line

master # tclsh fhutt.tcl
two

master # tclsh <<< 'puts $tcl_version'
8.5

Feherke.
http://feherke.github.com/

RE: List manipulation

(OP)
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

RE: List manipulation

Hi

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

there are many roads to...the solution:

set var1 "\{one two\}"
set new [lindex $var1 0 1]
Now new will contain two
 

RE: List manipulation

Hi

Yay ! I think it is time to re-read the whole Tcl documentation.

Good point thacoda. medal

Feherke.
http://feherke.github.com/

RE: List manipulation

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close