when I run a test like : Factorial 2
I got "wrong # args:should be "set varName ? newValue"
I dont know what's the reason, my TCL version is "Active state
activeTCL 8.4.14.0"
Thanks,
Harry
whitespace is important to the Tcl interpreter
your line:
set i 1;set product 1
you meant as 2 statement, separated by the semicolon.
but the interpreter separates words by spaces so it
sees:
set i "1;set" product 1
which it views as the set command with 4 arguments - which is your error.
try adding spaces around the ;
set i 1 ; set product 1
or else putting each statment on it's own line, or be tricky
and use the fact that the set command also returns the value
set i [set product 1]
Bruce
That is an incorrect statement. The tcl interpreter sees the semicolon
even though there are no spaces surrounding it. Lets keep our facts
straight!
% set i 1;set product 1
1
% puts "i: $i product: $product"
i: 1 product: 1
I believe the problem is that he is missing a { on the while statement
line. That gives a different error than he reports but all we really
have to go on is his actual code, and the code he posted does indeed
lack a curly brace at the end of the while statement.
--
Bryan Oakley
http://www.tclscripting.com
Tcl's usually very good about telling you exactly what is wrong (BTW,
you didn't get that error message with that code; it's wrong in other
ways! It's always useful when reporting problems with code to cut-and-
paste the offending code and any error messages instead of retyping
them so you don't *add* mistakes...) and in your case, it's saying
"too many arguments to set". Find somewhere where you've got too many
arguments (it can take one or two) and add in a command separator (a
newline or a semicolon; newlines are "good style").
As a guess, you've got a colon and not a semicolon in the first line
of the body of your real procedure. But I'm just guessing there... ;-)
Donal.
No. The interpreter sees ";" as a command separator. Has done for at
least 17 years. (I say "at least" because, though it is listed in
Tcl's "changes" file, it's in a part that is so old it is undated.
Puts it back before 3/19/90. :-))
Donal.
No, that's not it - and not true: ';' is a command separator alright:
% set x 1;set y 2
2
% set x
1
% set y
2
The problem in the *posted* code is a missing '{' (at the end of the
'while' line):
(orig)
% proc Factorial {x} {
set i 1;set product 1
while {$i<=$x}
set product [expr $product*$i]
incr i
}
return $product
} % can't read "product": no such variable
% Factorial 2
invalid command name "}"
(fixed)
% proc Factorial {x} {
set i 1;set product 1
while {$i<=$x} {
set product [expr $product*$i]
incr i
}
return $product
}
% Factorial 2
2
Given the error message that is reported, I assume that the code posted
was not copy/pasted but retyped - with at least one typo. Further
evidence: when typing at the console, you will get a space after the
prompt '%'; it is missing ;)
So, to the original poster: could you please show us *exactly* the
details of the problematic session? Then we will be able to help.
you're right of course, i shouldn't post on Monday mornings...
Bruce
> when I run a test like : Factorial 2
> I got "wrong # args:should be "set varName ? newValue"
>
> I dont know what's the reason, my TCL version is "Active state
> activeTCL 8.4.14.0"
The first thing I suggest is to try this - since the message says
there is a "set command" with the wrong number of arguments, you
should try typing the specific set commands in your code to the %
prompt of tcl and see which one fails. That would narrow down for you
which line was the problem.
There are tools available for helping debug Tcl code. See http://wiki.tcl.tk/473
for a variety of Tcl debugging tools that people have written about.
Thank you all for the kind assistances, i have located the problem,
now it works well.
Harry