0

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.

1
  • Is the original string A-B,C&D-E,F,G&H-I,J,K or does it have the parentheses and semicolon too? Be exact. It affects the exact answer you get. Commented Dec 3, 2013 at 18:06

2 Answers 2

1
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);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.