user2901871
user2901871

Reputation: 219

Creating String from list

I have a string like below:

 (A-B,C&D-E,F,G&H-I,J,K);

In the above string i used split command with "&" and got following elements :

 (A-B,C); (D-E,F,G) ;(H-I,J,K);

In the above element added Z in second element :

 (A-B,C); (D-E,F,G,Z); (H-I,J,K);

Now want to reconstruct string to original with z added for example :

 (A-B,C&D-E,F,G,Z&H-I,J,K);

Kindly share your suggestions thank you.

Upvotes: 0

Views: 77

Answers (2)

glenn jackman
glenn jackman

Reputation: 247210

set s "(A-B,C&D-E,F,G&H-I,J,K);"
set l [split $s "&"]
lset l 1 "[lindex $l 1],Z"
set new [join $l &]
puts $new
(A-B,C&D-E,F,G,Z&H-I,J,K);

Upvotes: 1

Marco Pallante
Marco Pallante

Reputation: 4043

Well, let's suppose you have the three elements in a list

puts $elements
{(A-B,C);} {(D-E,F,G,Z);} {(H-I,J,K);}

First of all, let's remove ( and ); from each element

set trimmed [list]

foreach e $elements {
    lappend trimmed [string trim $e "();"]
}

Now whe have a new list:

puts $trimmed
A-B,C D-E,F,G,Z H-I,J,K

Finally, let's join the list using the & character, and add again ( in front of the resulting string and ); at the end:

set final ([join $trimmed &])\;
puts $final
(A-B,C&D-E,F,G,Z&H-I,J,K);

This should be all.

Upvotes: 0

Related Questions