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

True and False in Tcl?

2,272 views
Skip to first unread message

Philip Newton

unread,
May 3, 2001, 10:19:54 AM5/3/01
to
Hi, I'm using Tcl 8.2.3 as embedded in Vignette StoryServer 5.0.

I have a question about what is recognised as true and false in an if
statement. The manpage I get has this to say:

"The value of the expression must be a boolean (a numeric value, where
0 is false and anything is true, or a string value such as true or yes
for true and false or no for false)"

However, the use of "such as" is not very satisfactory to me. Is there
an official list of what's true and what's false?

Are there values which are neither? Or is every non-true string false,
or maybe every non-false string is true?

Is 'y' true? How about 'j' or 'ja' or 'oui' or 'Yes' or 'trUe'? Is
there an official list of true and false values?

Cheers,
Philip
--
Philip Newton <nospam...@gmx.li>
Yes, that really is my address; no need to remove anything to reply.
If you're not part of the solution, you're part of the precipitate.

Chris Nelson

unread,
May 3, 2001, 10:43:13 AM5/3/01
to
Philip Newton wrote:
>
> Hi, I'm using Tcl 8.2.3 as embedded in Vignette StoryServer 5.0.
>
> I have a question about what is recognised as true and false in an if
> statement. The manpage I get has this to say:
>
> "The value of the expression must be a boolean (a numeric value, where
> 0 is false and anything is true, or a string value such as true or yes
> for true and false or no for false)"
>
> However, the use of "such as" is not very satisfactory to me. Is there
> an official list of what's true and what's false?
>
> Are there values which are neither? Or is every non-true string false,
> or maybe every non-false string is true?
>
> Is 'y' true? How about 'j' or 'ja' or 'oui' or 'Yes' or 'trUe'? Is
> there an official list of true and false values?

Logically true: 1, true, on, yes
Logically false: 0, false, off, no

Note that only recently has:

set x on
if { ! $x } {
...
}

Worked as expected.

Chris
--
As MIT is not "Massachusetts" neither is RPI "Rensselaer"

Don Porter

unread,
May 3, 2001, 10:49:47 AM5/3/01
to
Chris Nelson <ch...@pinebush.com> wrote:
> Note that only recently has:
>
> set x on
> if { ! $x } {
> ...
> }
>
> Worked as expected.

...where "recently" means "in the Tcl 8.3.3 release".

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

Donal K. Fellows

unread,
May 3, 2001, 11:19:06 AM5/3/01
to
Philip Newton wrote:
> However, the use of "such as" is not very satisfactory to me. Is there
> an official list of what's true and what's false?

See the Tcl_GetBoolean(3) manpage.

Donal.
--
Donal K. Fellows http://www.cs.man.ac.uk/~fellowsd/ fell...@cs.man.ac.uk
-- US citizens? Remember, I rule the world in this scenario. They aren't
citizens of the US, unless that stands for United Stevenland.
-- Steven Odhner <ta...@primenet.com>

Jeffrey Hobbs

unread,
May 3, 2001, 12:22:25 PM5/3/01
to
Philip Newton wrote:
>
> Hi, I'm using Tcl 8.2.3 as embedded in Vignette StoryServer 5.0.
>
> I have a question about what is recognised as true and false in an if
> statement. The manpage I get has this to say:
>
> "The value of the expression must be a boolean (a numeric value, where
> 0 is false and anything is true, or a string value such as true or yes
> for true and false or no for false)"
>
> However, the use of "such as" is not very satisfactory to me. Is there
> an official list of what's true and what's false?
>
> Are there values which are neither? Or is every non-true string false,
> or maybe every non-false string is true?
>
> Is 'y' true? How about 'j' or 'ja' or 'oui' or 'Yes' or 'trUe'? Is
> there an official list of true and false values?

Look at the Tcl_GetBoolean docs, but also in the shell you have
string is true -strict $string
string is false -strict $string
string is boolean -strict $string

which is basically a wrapper around Tcl_GetBoolean. The -strict
is necessary to avoid the empty string being evaluated to true.

--
Jeff Hobbs The Tcl Guy
Senior Developer http://www.ActiveState.com/
Tcl Support and Productivity Solutions

Glenn Jackman

unread,
May 3, 2001, 1:01:35 PM5/3/01
to
Don Porter <d...@clover.cam.nist.gov> wrote on [3 May 2001 14:49:47 GMT]:
>Chris Nelson <ch...@pinebush.com> wrote:
>> Note that only recently has:
>>
>> set x on
>> if { ! $x } {
>> ...
>> }
>>
>> Worked as expected.
>
>...where "recently" means "in the Tcl 8.3.3 release".

Can someone explain the following:

% if {true} {puts yes} else {puts no}
syntax error in expression "true"
% set t true
true
% if {$t} {puts yes} else {puts no}
yes
% expr {true}
syntax error in expression "true"
% expr {$t}
true
% set tcl_patchLevel
8.3.3

Why does the plain string {true} not evaluate to true?

--
Glenn Jackman

Don Porter

unread,
May 3, 2001, 1:54:51 PM5/3/01
to
Glenn Jackman <gle...@nortelnetworks.com> wrote:
> Can someone explain the following:
>
> % if {true} {puts yes} else {puts no}
> syntax error in expression "true"
> % set t true
> true
> % if {$t} {puts yes} else {puts no}
> yes
> % expr {true}
> syntax error in expression "true"
> % expr {$t}
> true
> % set tcl_patchLevel
> 8.3.3
>
> Why does the plain string {true} not evaluate to true?

[expr] tries in interpret "bare" strings as function names, such
as cos($x). You must quote the string "true". Then it behaves
as you expect in 8.3.3.

% if {"true"} {puts yes} {puts no}
yes
% expr {"true"}
true
% expr {!"true"}
0

Kristoffer Lawson

unread,
May 3, 2001, 4:23:49 PM5/3/01
to
Don Porter <d...@clover.cam.nist.gov> wrote:

: [expr] tries in interpret "bare" strings as function names, such


: as cos($x). You must quote the string "true". Then it behaves
: as you expect in 8.3.3.

I'll probably offend a few people here, but: yuck.

--
- ---------- = = ---------//--+
| / Kristoffer Lawson | www.fishpool.fi|.com
+-> | se...@fishpool.com | - - --+------
|-- Fishpool Creations Ltd - / |
+-------- = - - - = --------- /~setok/

Don Porter

unread,
May 3, 2001, 5:33:21 PM5/3/01
to
Don Porter <d...@clover.cam.nist.gov> wrote:
>: [expr] tries in interpret "bare" strings as function names, such
>: as cos($x). You must quote the string "true". Then it behaves
>: as you expect in 8.3.3.

Kristoffer Lawson <se...@fishpool.com> wrote:
> I'll probably offend a few people here, but: yuck.

I hear you, but in real programming (rather than cute examples) how
often do you really include a literal "true" or "false" in your
expressions?

Those who try to extend [expr] with their own functions see their
frown turn upside-down.

Kristoffer Lawson

unread,
May 3, 2001, 6:26:38 PM5/3/01
to
Don Porter <d...@clover.cam.nist.gov> wrote:

: Kristoffer Lawson <se...@fishpool.com> wrote:
:> I'll probably offend a few people here, but: yuck.

: I hear you, but in real programming (rather than cute examples) how
: often do you really include a literal "true" or "false" in your
: expressions?

Well actually I did come across the problem mentioned here in another
person's code. It's always awkward when you can't find a real explanation
for why something is the way it is.

: Those who try to extend [expr] with their own functions see their
: frown turn upside-down.

Hm, yes, that does make a bit of sense. Of course we wouldn't be having
this discussion if expr functions were Tcl commands instead ;-)

Michael Esveldt

unread,
May 3, 2001, 10:26:04 PM5/3/01
to
> Note that only recently has:
>
> set x on
> if { ! $x } {
> ...
> }
>
> Worked as expected.

What exactly is expected? I understand what is returned and why it works
because I fiddled with it (after long years of irritation at Tcl boolean
handling) but it makes no sense to me in terms of practicality.

How did Tcl come to handle booleans in this fashion?

Michael

--
The Chao says Mu.
Michael Esveldt, #FightThePower on irc.openprojects.net
<mailto:da...@oz.net> <http://velocity.editthispage.com/>

Richard.Suchenwirth

unread,
May 4, 2001, 2:52:38 AM5/4/01
to
Michael Esveldt wrote:
>
> > Note that only recently has:
> >
> > set x on
> > if { ! $x } {
> > ...
> > }
> >
> > Worked as expected.
>
> What exactly is expected?

That negated truth values are still truth values.

> I understand what is returned and why it works
> because I fiddled with it (after long years of irritation at Tcl boolean
> handling) but it makes no sense to me in terms of practicality.
>
> How did Tcl come to handle booleans in this fashion?

Seen from C, 0 is boolean 'false', every other number is boolean 'true'.
This simple behavior was inherited to Tcl.
In addition, as "everything is a string", certain strings (true, yes,
on; false, no, off) were allowed as aliases for the truth values. This
goes beyond what you can do in C:
if("false") {
...
}
is always true, since the pointer to a string constant is not NULL...
--
Schoene Gruesse/best regards, Richard Suchenwirth - +49-7531-86 2703
Siemens Production and Logistics Systems, PA RC D2, Konstanz,Germany
Personal opinions expressed only unless explicitly stated otherwise.

Philip Newton

unread,
May 4, 2001, 2:59:41 AM5/4/01
to
On Thu, 03 May 2001 16:22:25 GMT, Jeffrey Hobbs
<Je...@ActiveState.com> wrote:

> Look at the Tcl_GetBoolean docs,

Thanks Jeffrey (and Donal, who pointed me at the same place, and also
to Chris, who gave a list of values but didn't say where they came
from). That helped.

> but also in the shell you have
> string is true -strict $string
> string is false -strict $string
> string is boolean -strict $string

Ah! Interesting. Thank you very much.

Donal K. Fellows

unread,
May 4, 2001, 5:10:42 AM5/4/01
to
Kristoffer Lawson wrote:
> Don Porter <d...@clover.cam.nist.gov> wrote:
>: [expr] tries in interpret "bare" strings as function names, such
>: as cos($x). You must quote the string "true". Then it behaves
>: as you expect in 8.3.3.
>
> I'll probably offend a few people here, but: yuck.

It's fixed in the CVS HEAD; it missed the 8.3.3 cut by about a day.

-- OK, there is the MFC, but it only makes the chaos object orientated.
-- Thomas Nellessen <nell...@gmx.de>

Donal K. Fellows

unread,
May 4, 2001, 5:18:45 AM5/4/01
to
Michael Esveldt wrote:
> How did Tcl come to handle booleans in this fashion?

The expression code that handled parsing and negating booleans (written for
8.0a1 maybe?) was incomplete in that it didn't handle the string forms of
bools. I've been adding this functionality back into the core. 8.3.3 has
the "run-time" understanding of string-bools (believe me, that's quite
tricky to handle right) and the next release of 8.3 or 8.4 will have the
parsing as well (sorry, but work didn't free up enough time to fix them
both at the same time and what with one important thing and another, the
second portion of the fix missed the cut.)

If you want to update Tcl8.3.3 to a version that supports string boolean
literals in expressions, grab tcl/generic/tclParseExpr.c out of the CVS
tree and rebuild from source...

This is a known and *fixed* bug. HTH.

Chris Nelson

unread,
May 4, 2001, 7:57:03 AM5/4/01
to
Philip Newton wrote:
> ... and also to Chris, who gave a list of values but didn't say
> where they came from). ...

They came from page 4 of my book
(http://www.purl.org/net/TclTkProgRef). They got _there_ because I
played around until I understood it; now I can just look it up.

Chris
--
"Custody" and "visitation" are for prisoners.
http://assembly.state.ny.us/leg/?bn=A01920&sh=t

lvi...@yahoo.com

unread,
May 4, 2001, 12:06:45 PM5/4/01
to

According to Kristoffer Lawson <se...@fishpool.com>:

:Don Porter <d...@clover.cam.nist.gov> wrote:
:
:: [expr] tries in interpret "bare" strings as function names, such
:: as cos($x). You must quote the string "true". Then it behaves
:: as you expect in 8.3.3.
:
:I'll probably offend a few people here, but: yuck.

Since it is my preference to see _all_ strings quoted (for that matter,
to see Tcl _REQUIRE_ all strings be quoted), the yuck in my
mind is trying to use true or false without the quotes ;-) ...

--
--
"See, he's not just anyone ... he's my son." Mark Schultz
<URL: mailto:lvi...@cas.org> <URL: http://www.purl.org/NET/lvirden/>
Even if explicitly stated to the contrary, nothing in this posting

Larry Smith

unread,
May 4, 2001, 2:11:54 PM5/4/01
to
Kristoffer Lawson wrote:
>
> Don Porter <d...@clover.cam.nist.gov> wrote:
>
> : [expr] tries in interpret "bare" strings as function names, such
> : as cos($x). You must quote the string "true". Then it behaves
> : as you expect in 8.3.3.
>
> I'll probably offend a few people here, but: yuck.

No, I tend to agree. The quotes really should mean
a string. I ran into much the same problem with my
"compute" extension to expr. I solved it with a post-
processing pass that takes care of true and false (and
yes and no, and on and off). Unfortunately, while this
extension of expr works nicely with expr, it doesn't
work at all with if or while - the docs _say_ that if
and while just call expr, but when expr is replaced by
something else, they continue to call the original.
Most unorthogonal, and IMHO untclish.
--
.-. .-. .---. .---. .-..-. | Do not mistake my cynicism
| |__ / | \| |-< | |-< > / | for dispair for deep inside
`----'`-^-'`-'`-'`-'`-' `-' | me is an optimist forever
http://www.smith-house.org/ | asking, "Why not?"

Richard.Suchenwirth

unread,
May 7, 2001, 2:02:52 AM5/7/01
to
lvi...@yahoo.com wrote:
> Since it is my preference to see _all_ strings quoted (for that matter,
> to see Tcl _REQUIRE_ all strings be quoted), the yuck in my
> mind is trying to use true or false without the quotes ;-) ...

But then you'd end up with a tremendous lots of quotes, since just about
everything is a string. In contrast to most other languages, Tcl quotes
are for grouping (like braces, but with evaluation).
One could make it a stylistic issue, though: recommend that single words
are quoted if they are _really_ meant to be a string.. but somehow I
come to no definition what is a _real string_ in Tcl...

However, requiring quotes does not seem the Tcl way to me.

Donal K. Fellows

unread,
May 8, 2001, 5:54:59 AM5/8/01
to
Larry Smith wrote:
> No, I tend to agree. The quotes really should mean a string. I ran into
> much the same problem with my "compute" extension to expr. I solved it
> with a post-processing pass that takes care of true and false (and yes and

> no, and on and off). Unfortunately, while this extension of expr works
> nicely with expr, it doesn't work at all with if or while - the docs _say_
> that if and while just call expr, but when expr is replaced by something
> else, they continue to call the original. Most unorthogonal, and IMHO
> untclish.

[if] and [while] call the internal [expr] implementation[*] and have done
for as many years as I've been using Tcl...

Donal.
[* When not bytecoded away, of course. ]

-- Of course, she was referring not to the current but to a past chair, unless
Babbage is very, very old, *and* tied to a chair in Hawking's basement.
Which, admittedly, would be pretty cool. -- Chris Hammock

0 new messages