Reply
Contributor
Tanvir
Posts: 35
Registered: 09-18-2008
0

Some clarification on using curly braces

Hello,

 

I'm trying to work with some list vars and finding that I have to experiment with where and if I use curly braces.  Not being really familiar with Tcl syntax, I was wondering if someone could just give me a brief overview of the differences between:

 

$var

${var}

{$var}

 

Thanks.

Expert
KumarS
Posts: 2,233
Registered: 08-30-2008
0

Re: Some clarification on using curly braces

$var and ${var} are identical. Braces around the variable name are used in case your variable is a complex one e.g. ${//some_xpath}.

 

Suppose "var" value was 5. Surrounding something will braces stops susbtitution. So when you say {$var}, it is requivalent to the string "$var" as value not "5".

 

So:

puts $var

will produce

5

 

puts ${var}

will produce

5

 

puts {$var}

will produce

$var

 

Generally to build a complex string, you can use two types of syntax:

1. one where you surround the string with double quotes e.g.

set i "lazy fox jumped over $var"

2. one where you surround the string with braces e.g.

set i {lazy fox jumped over $var}

 

In the first one, i will be set to 'lazy fox jumped over 5'

In the second one, i will be set to 'lazy fox jumped over $var'

 

Hope this makes things clear.