Subject: Re: Tcl "expr" command change suggestion - DN [1]
"Richard.Suchenwirth" <Richard.Suchenwirth@kst.siemens.de> - 08 Jul 1999 - comp.lang.tcl
Costas Menico wrote:
> A command in Tcl is enclosed in [ ].
Sometimes, yes. If its return value shall be used in another command.
> It is not enclosed in { }.
What about "foreach i {red green blue} {puts hello,$i}"
So the requirement is that you would still use the command brackets.
> E.g. [expr 1 + 1] can be [1 + 1]
A command has a name and a sequence of arguments. In your example,
command "1" (legal name) would be called with arguments "+ 1".
As they used to say before Tcl 8.0, "everything is a string" - depends
what you do with it. The few syntax rules in Tcl(n) are enough, and good
enough, for me, and a number of other folks. Still, in Tcl you're free
to do many things you wouldn't even start in other languages.
Introspection. Write a C program that interactively tells you what
functions it contains.
And, as you're interested in alternatives to expr, here's my unknown
proc:
if ![info proc _unknown] {rename unknown _unknown} ;# keep the original
proc unknown {args} {
if [regexp (.+):$ [lindex $args 0] -> name] {
set args [lreplace $args 0 0 $name =]
} ;# allow REBOL-style assignments (foo: bar; bar: 17+4)
if {[lindex $args 1]=="="} {
# maybe an assignment like "x = 3+4" ? (Blanks matter!)
upvar [lindex $args 0] _x
set rest [lrange $args 2 end]
if [llength [info commands [lindex $args 2]]] {
return [set _x [uplevel eval $rest]]
}
set _x $rest ;# this should always work...
catch {set _x [expr $rest]} ;# ...but maybe expr is happy
return $_x
} elseif {[regexp {^([^ ]+)\+\+$} $args -> vname]} {
uplevel [list incr $vname] ;# allow things like "i++" ...
} elseif {[regexp {^([^ ]+)--$} $args -> vname]} {
uplevel [list incr $vname -1] ;# ... or "j--"
} elseif {[regexp {^[-+/\*\.0-9 ()]+$} $args]} {
return [expr $args] ;# pure expression? "(17+4)/3"
} else {eval _unknown $args} ;# let old "unknown" do it
}
For calls like
v = info tclversion ;# maybe assign a proc result (no []!!)
8.1
s = this is a string ;# default: shrink-wrapped string (no ""!)
this is a string
j = sqrt(2)*3 ;# if expr agrees, the result (no [expr..]!)
4.24264068712
Yes, this is still Tcl, and no, it's not like in the book.
--
Schoene Gruesse/best regards, Richard Suchenwirth -- tel. +49-7531-86
2703
> RC DT2, Siemens Electrocom GmbH, Buecklestr. 1-5, D-78467 Konstanz, Germany
> My opinions were not necessarily, or will not necessarily be, mine.
Last modified
1999-09-27
1999-09-27
(195.108.246.50)
Note: you are looking at
the snapshot of an old wiki
- much of this information
is likely to be very outdated
