Divesh Rastogi
Divesh Rastogi

Reputation: 49

Concatenation inserting additional space character while joining the list in tcl. Is there a way to remove it?

I am trying to split a variable and wishes to add a suffix to the first field and then later concatenate it with 2nd one using tcl

like

san_jose

to

san_state_jose

I can split the code

   set tcl_list [split $var_name "_"]
   set new_name [concat [lindex $tcl_list 0] "_state_" [lindex $tcl_list 1]]
   puts $new_name

But concatenation unfornatutly inserts additional space character before combining them as shown below

   san _state_ jose

Could you please help me correcting that?

-Regards, Div

Upvotes: 0

Views: 351

Answers (3)

vibha
vibha

Reputation: 1

set tcl_list [split san_jose "_"]
set new_name [concat [lindex $tcl_list 0]_state_[lindex $tcl_list 1]]
puts $new_name

Upvotes: 0

Shawn
Shawn

Reputation: 52579

One way is to use string cat:

set new_name [string cat [lindex $tcl_list 0] "_state_" [lindex $tcl_list 1]]

Or if new_name doesn't already exist, append:

append new_name [lindex $tcl_list 0] "_state_" [lindex $tcl_list 1]

Or just good old interpolation:

 set new_name "[lindex $tcl_list 0]_state_[lindex $tcl_list 1]"

Or maybe linsert and join:

set new_name [join [linsert $tcl_list 1 state] _]

Upvotes: 1

Divesh Rastogi
Divesh Rastogi

Reputation: 49

I can solve the problem using regsub but I am interested in learning other ways also.

   set new_name [regsub {_} $var_name "_state_"]
   puts $new_name
   

-Regards, D

Upvotes: 0

Related Questions