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

Im sure you guys could answer me quickly. whats the difference

9 views
Skip to first unread message

Tricky

unread,
Oct 5, 2011, 11:05:03 AM10/5/11
to
Ok, I dont quite get the difference between these lines of code,
namely the a,b,c variables.

set hw "Hello world!"

set a $hw
set b "$hw"
set c "${hw}"

puts "$a $b $c"

exit

Can anyone explain the difference?

Don Porter

unread,
Oct 5, 2011, 11:18:06 AM10/5/11
to
What difference? Variables a, b, and c all hold identical values.

--
| Don Porter Applied and Computational Mathematics Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

scottdware

unread,
Oct 5, 2011, 11:20:17 AM10/5/11
to
You're simply just applying your hw variable to others (variable substitution...i.e. $a, $b, $c). Now, if you were to do something like the following:

set d {$hw}

That wouldn't work because no substitution takes place inside {}. See:

http://docs.activestate.com/activetcl/8.5/tcl/TclCmd/Tcl.htm#M12

Most notably, the part where it explains variable substitution.

Glenn Jackman

unread,
Oct 5, 2011, 11:40:27 AM10/5/11
to
In this case, no difference at all.

However, if you wanted the new variable to hold "Hello world!_abc def", then

set a "$hw_abc def" ;# fails, no such variable "hw_abc"

set b "${hw}_abc def" ;# success



--
Glenn Jackman
Write a wise saying and your name will live forever. -- Anonymous

Tricky

unread,
Oct 5, 2011, 11:19:34 AM10/5/11
to
On Oct 5, 4:18 pm, Don Porter <d...@nist.gov> wrote:
> Tricky wrote:
> > Ok, I dont quite get the difference between these lines of code,
> > namely the a,b,c variables.
>
> > set hw "Hello world!"
>
> > set a $hw
> > set b "$hw"
> > set c "${hw}"
>
> > puts "$a $b $c"
>
> > exit
>
> > Can anyone explain the difference?
>
> What difference?  Variables a, b, and c all hold identical values.
>
> --
> | Don Porter            Applied and Computational Mathematics Division |
> | donald.por...@nist.gov             Information Technology Laboratory |
> |http://math.nist.gov/~DPorter/                                 NIST |
> |______________________________________________________________________|

This came about because I saw this in another tcl file:

set proj_name "${myProject}.$ext"

I didnt understand why the {} were needed around the myProject
variable.

Zbiggy

unread,
Oct 5, 2011, 12:33:54 PM10/5/11
to
In comp.lang.tcl, Tricky wrote:

> set proj_name "${myProject}.$ext"
>
> I didnt understand why the {} were needed around the myProject
> variable.

Could be protection in case, if $myProject would contain any spaces.
--
Z.

Don Porter

unread,
Oct 5, 2011, 12:56:02 PM10/5/11
to
Tricky wrote:
> set proj_name "${myProject}.$ext"
>
> I didnt understand why the {} were needed around the myProject
> variable.

Why didn't you just ask that then?

The braces are not needed in that example. Some programmers have
styles either chosen or enforced that add syntax that's not
necessary, but harmless.

--
| Don Porter Applied and Computational Mathematics Division |
| donald...@nist.gov Information Technology Laboratory |
| http://math.nist.gov/~DPorter/ NIST |
|______________________________________________________________________|

Robert Heller

unread,
Oct 5, 2011, 1:53:00 PM10/5/11
to
The different sorts of quoting are only meaning full in various sorts of
contexts. For example, in case b, the use of double quotes would only
be needed if there was more going on, such as:


set b "Frank woke up and said, '$hw'"

The statement:

set b Frank woke up and said, '$hw'

would raise an error (too many argumets to set).

Case c handles a different case, where the hw dereference would be
followed by an additional 'word' character:

set c "${hw}x"

Leaving off the braces would yield:

set c "$hwx"

which would be an error, since there is no variable named "hwx". The
braces separate the variable "hw" from the random "x" following.


--
Robert Heller -- 978-544-6933 / hel...@deepsoft.com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments



Mark Janssen

unread,
Oct 6, 2011, 4:37:07 PM10/6/11
to
On Oct 5, 6:56 pm, Don Porter <d...@nist.gov> wrote:
> Tricky wrote:
> > set proj_name "${myProject}.$ext"
>
> > I didnt understand why the {} were needed around the myProject
> > variable.
>
> Why didn't you just ask that then?
>
> The braces are not needed in that example.  Some programmers have
> styles either chosen or enforced that add syntax that's not
> necessary, but harmless.
>
> --
> | Don Porter            Applied and Computational Mathematics Division |
> | donald.por...@nist.gov             Information Technology Laboratory |
> |http://math.nist.gov/~DPorter/                                 NIST |
> |______________________________________________________________________|

See http://www.tcl.tk/man/tcl8.5/TclCmd/Tcl.htm rule 8 for the details.
0 new messages