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.
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"
...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 |
|______________________________________________________________________|
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>
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
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
[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
: [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/
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 <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 ;-)
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/>
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.
> 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.
It's fixed in the CVS HEAD; it missed the 8.3.3 cut by about a day.
Donal.
--
Donal K. Fellows http://www.cs.man.ac.uk/~fellowsd/ fell...@cs.man.ac.uk
-- OK, there is the MFC, but it only makes the chaos object orientated.
-- Thomas Nellessen <nell...@gmx.de>
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.
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
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
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?"
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.
[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. ]
--
Donal K. Fellows http://www.cs.man.ac.uk/~fellowsd/ fell...@cs.man.ac.uk
-- 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