Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

variable substitution within quotes in braces

18 views
Skip to first unread message

abhilasha21

unread,
May 4, 2007, 7:43:58 AM5/4/07
to
Hi All ,

I have been trying to find a way to substitute a variable within
quotes within braces..
eg : i need to pass following string as an argument to a procedure.

{<ab:max><ab:mid="xyz:abc...@pqrs.com"/></wyty:ert>}

I want to assign ' xyz:abc...@pqrs.com' to a variable say abhi.

so my string should look like :
{<ab:max><ab:mid="$abhi"/></wyty:ert>} .

Can anyone please help me to solve this trick in TCl..?


Thanks in advance ...
Abhilasha

miguel sofer

unread,
May 4, 2007, 8:05:03 AM5/4/07
to


Does this answer your question?

% set x xyz:abc...@pqrs.com
xyz:abc...@pqrs.com
% set y {<ab:max><ab:mid="$x"/></wyty:ert>}
<ab:max><ab:mid="$x"/></wyty:ert>
% set z [subst -nocommands $y]


<ab:max><ab:mid="xyz:abc...@pqrs.com"/></wyty:ert>

If not, maybe the following does:

% set y {{<ab:max><ab:mid="$x"/></wyty:ert>}}
{<ab:max><ab:mid="$x"/></wyty:ert>}
% set z [subst -nocommands $y]

Bryan Oakley

unread,
May 4, 2007, 9:35:38 AM5/4/07
to

Someone else has already mentioned subst; does simply changing the
quoting mechanism not work for you?

set string "<ab:max><ab:mid=\"$abhi\"/></wyty:ert>"

This can, of course, cause you to generate invalid xml if $abhi contains
xml-special characters (such as unbalanced <'s, "'s, etc) but
presumably you are taking care of that.

--
Bryan Oakley
http://www.tclscripting.com

Ralf Fassel

unread,
May 4, 2007, 11:03:41 AM5/4/07
to
* abhilasha21 <abhilash...@gmail.com>

| I want to assign ' xyz:abc...@pqrs.com' to a variable say abhi.
|
| so my string should look like :
| {<ab:max><ab:mid="$abhi"/></wyty:ert>} .
|
| Can anyone please help me to solve this trick in TCl..?

Build it in pieces:
set abhi xyz:abc...@pqrs.com
set result {}
append result {<ab:max><ab:mid="} $abhi {"/></wyty:ert>}
=> <ab:max><ab:mid="xyz:abc...@pqrs.com"/></wyty:ert>

HTH
R'

sleb...@gmail.com

unread,
May 4, 2007, 12:28:37 PM5/4/07
to

Subst and \" escaping are of course good solutions. My personal
favourite string substitution method when I have to deal with strings
containing special characters is [string map]:

string map [list %ABHI% $abhi] {<ab:max><ab:mid="%ABHI%"/></
wyty:ert>}

Of course since [string map] is straightforward byte-by-byte
translation you can choose any pattern to mark where you want the
substitution without affecting the rest of the string:

string map [list ... $abhi] {<ab:max><ab:mid="..."/></wyty:ert>}

bs

unread,
May 4, 2007, 5:02:45 PM5/4/07
to

I know HTML you can use single quotes instead of double quotes...not
sure if that works for you here or not, but it would look something
like:

set str "<ab:max><ab:mid='$abhi'/></wyty:ert>"

HTH,
--brett

Schelte Bron

unread,
May 4, 2007, 6:21:33 PM5/4/07
to
abhilasha21 wrote:
> I have been trying to find a way to substitute a variable within
> quotes within braces..
> eg : i need to pass following string as an argument to a
> procedure.
>
> {<ab:max><ab:mid="xyz:abc...@pqrs.com"/></wyty:ert>}
>
> I want to assign ' xyz:abc...@pqrs.com' to a variable say abhi.
>
In situations like these I prefer to use format:
set result [format {{<ab:max><ab:mid="%s"/></wyty:ert>}} $abhi]

But also the following will work:
set result "{<ab:max><ab:mid=\"$abhi\"/></wyty:ert>}"

Schelte.
--
set Reply-To [string map {nospam schelte} $header(From)]

Ralf Fassel

unread,
May 5, 2007, 9:42:50 AM5/5/07
to
* Schelte Bron <nos...@wanadoo.nl>

| But also the following will work:
| set result "{<ab:max><ab:mid=\"$abhi\"/></wyty:ert>}"

You might want to omit the leading and trailing {} in this case.

R'

Schelte Bron

unread,
May 5, 2007, 11:05:10 AM5/5/07
to
Ralf Fassel wrote:

That depends how you interpret the question. Since the subject
indicated "quotes in braces" I assumed the OP wanted the braces.
Without that requirement the question would have been no challenge
at all ;-)

Ralf Fassel

unread,
May 5, 2007, 11:09:37 AM5/5/07
to
* Schelte Bron <nos...@wanadoo.nl>

| Since the subject indicated "quotes in braces" I assumed the OP
| wanted the braces.

I assumed the OP wanted the quotes in braces, but not the braces in
quotes ;-)

Seriously, since the braces are not part of the string if the
set foo {...}
form is used, I would assume that if you use
set foo "..."
there should be no braces.

R'

0 new messages