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

getting rid of '{}'

1,374 views
Skip to first unread message

anuj

unread,
Jan 4, 2006, 7:59:12 AM1/4/06
to
Hello,

I am using the 'list' data structure to build up a SIP INVITE message.
When I do that, the final message that is built has lotsa '{' braces in
it. How do I get rid of them?

A workaround could be to use 'lreplace', but it needs me to enter the
exact position/index value of the braces (which I can get using
lsearch).

I just wanted to know if there is a cleaner solution.

Best Regards,
Anuj Mistry.

Gerald W. Lester

unread,
Jan 4, 2006, 8:42:00 AM1/4/06
to

You need to provide us an example of what the data and what the commands you
are using to get these "unwanted braces". Also provide us with what you
want to have at the end.

Unless a SIP INVITE message is a Tcl List, there is a good change you should
not be using list commands, but rather string commands to build up the message.

suchenwi

unread,
Jan 4, 2006, 8:40:56 AM1/4/06
to
42 % string map {\{ "" \} ""} "{this is {an example {of nested
braces}}}"
this is an example of nested braces

Bryan Oakley

unread,
Jan 4, 2006, 9:04:42 AM1/4/06
to
anuj wrote:
> Hello,
>
> I am using the 'list' data structure to build up a SIP INVITE message.
> When I do that, the final message that is built has lotsa '{' braces in
> it. How do I get rid of them?

Generally speaking, if you have a bunch of curly braces that are
magically appearing and you want to get rid of them, you're doing
something wrong. Either you're creating a list where you shouldn't be,
or using a list as a string without explicitly converting it to a string.

The question is, do you need to use a list structure in the first place?
If so, you need to explicitly convert the list to a string when you
finally create your SIP INVITE message. That can be as simple as [join
$list " "], or as complex as a loop that iterates over nested lists,
piecing together your final string one word at a time.

Of course, the other workaround is to do [string map [list \{ "" \} ""]
$list], but that is exactly that -- a workaround. If you use Tcl's data
structures properly you should never have to manually remove curly braces.

anuj

unread,
Jan 4, 2006, 9:25:18 AM1/4/06
to
Hi Gerald,

Thanks.

The task at hand is to accept different parameters from the user (GUI)
and to assemble them to build the complete message. This message is
then sent out of a specified port.

I had a choice between using plain string and lists. I started off with
lists to build up the message. I declared an empty list and went on to
append (lappend) the different components of the message to the list.
So each component of the message is an item in the list. But when I
view the final (big) list, each individual component within the list is
enclosed within braces which I dont want.

Suchenwi,

The string map option seems to be good enough. Thanks.

The syntax says -

string map ?-nocase? charMap string

If I use -

string map {\{ "" \} ""} $reqmsg

it doesnt really knock ff the braces. It keeps the 'reqmsg' as it is.
Is it because 'reqmsg' is a list and not a string?

Thanks again.

Regards,
Anuj.

Bryan Oakley

unread,
Jan 4, 2006, 9:56:02 AM1/4/06
to
anuj wrote:
> Hi Gerald,
>
> Thanks.
>
> The task at hand is to accept different parameters from the user (GUI)
> and to assemble them to build the complete message. This message is
> then sent out of a specified port.
>
> I had a choice between using plain string and lists. I started off with
> lists to build up the message. I declared an empty list and went on to
> append (lappend) the different components of the message to the list.
> So each component of the message is an item in the list. But when I
> view the final (big) list, each individual component within the list is
> enclosed within braces which I dont want.

If that's the case, the solution is as simple as [join $list " "]. That
is, explicitly join every list element together with a space between them.

bs

unread,
Jan 4, 2006, 9:58:13 AM1/4/06
to

>
> The string map option seems to be good enough. Thanks.
>
> The syntax says -
>
> string map ?-nocase? charMap string
>
> If I use -
>
> string map {\{ "" \} ""} $reqmsg
>
> it doesnt really knock ff the braces. It keeps the 'reqmsg' as it is.
> Is it because 'reqmsg' is a list and not a string?
>

How are you actually viewing the braces in the list? Does the server
see these, or are you doing a [puts] with the list?

If you print a list, what you see on the console is a string rep of
that list, so it might not be necessarily true what you see.

% set mylist [list "with a space" "" hello]
{with a space} {} hello
% puts [lindex $mylist 0]
with a space
% puts [lindex $mylist 1]

% puts [lindex $mylist 2]
hello
% puts $mylist
{with a space} {} hello
%

Ronnie Brunner

unread,
Jan 4, 2006, 10:15:24 AM1/4/06
to
> I had a choice between using plain string and lists. I started off with
> lists to build up the message. I declared an empty list and went on to
> append (lappend) the different components of the message to the list.
> So each component of the message is an item in the list. But when I
> view the final (big) list, each individual component within the list is
> enclosed within braces which I dont want.

As Bryan wrote:

join $reqmsg " "

or just append instead of lappend your items when building up your message:

append reqmsg " " $item

> If I use -
>
> string map {\{ "" \} ""} $reqmsg
>
> it doesnt really knock ff the braces. It keeps the 'reqmsg' as it is.
> Is it because 'reqmsg' is a list and not a string?

string map takes a string not a variable to manipulate (that's why you
put $reqmsg not reqmsg).

set reqmsg [string map {\{ "" \} ""} $reqmsg]

will change your variable reqmsg...

hth
Ronnie

sleb...@gmail.com

unread,
Jan 4, 2006, 6:51:46 PM1/4/06
to
anuj wrote:
> If I use -
>
> string map {\{ "" \} ""} $reqmsg
>
> it doesnt really knock ff the braces. It keeps the 'reqmsg' as it is.
> Is it because 'reqmsg' is a list and not a string?
>

Anuj, one thing you should understand. A lot of tcl's standard
functions are purely functional, that is they don't modify their
parameters but return the result instead (there are exceptions of
course, like the incr command). So to use most of the advice given here
you have to set a variable to the return value of the function:

set reqmsg [string map {\{ "" \} ""} $reqmsg]

or for Bryan's solution:

set reqmsg [join $reqmsg " "]

Seasoned tclers will often just mention the function and assume that
you know how to 'set'.

anuj

unread,
Jan 5, 2006, 12:29:02 AM1/5/06
to
Thanks everyone for the help.

Yes, I am a newbie, and I skipped the use of 'set'.

Feels great to be able to access/talk to this TCL experts community!!!

0 new messages