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

question about a proc

14 views
Skip to first unread message

mike.h...@gmail.com

unread,
Aug 13, 2007, 11:56:52 AM8/13/07
to
Hi there,
I am learning TCL right now, i created a simple proc following the
book "practical programming in TCL and TK" the code is as following:
%proc Factorial {x} {
set i 1;set product 1
while {$i<=$x}
set product [expr $product*$i]
incr i
}
return $product
}

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

Bruce Hartweg

unread,
Aug 13, 2007, 12:01:28 PM8/13/07
to

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

Bryan Oakley

unread,
Aug 13, 2007, 12:20:04 PM8/13/07
to
Bruce Hartweg wrote:
> mike.h...@gmail.com wrote:
>> Hi there,
>> I am learning TCL right now, i created a simple proc following the
>> book "practical programming in TCL and TK" the code is as following:
>> %proc Factorial {x} {
>> set i 1;set product 1
>> while {$i<=$x}
>> set product [expr $product*$i]
>> incr i
>> }
>> return $product
>> }
>>
>> 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

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

Donal K. Fellows

unread,
Aug 13, 2007, 12:26:52 PM8/13/07
to
mike.hfzh...@gmail.com wrote:
> %proc Factorial {x} {
> set i 1;set product 1
> while {$i<=$x}
> set product [expr $product*$i]
> incr i
>
> }
> return $product
> }
>
> when I run a test like : Factorial 2
> I got "wrong # args:should be "set varName ? newValue"

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.

Donal K. Fellows

unread,
Aug 13, 2007, 12:30:49 PM8/13/07
to
Bruce Hartweg wrote:
> 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.

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.

miguel

unread,
Aug 13, 2007, 12:31:11 PM8/13/07
to

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.

Bruce Hartweg

unread,
Aug 13, 2007, 12:30:22 PM8/13/07
to

you're right of course, i shouldn't post on Monday mornings...

Bruce

Larry W. Virden

unread,
Aug 13, 2007, 12:40:54 PM8/13/07
to
On Aug 13, 11:56 am, mike.hfzh...@gmail.com wrote:

> 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.

mike.h...@gmail.com

unread,
Aug 13, 2007, 12:58:34 PM8/13/07
to
On Aug 13, 9:01 am, Bruce Hartweg <doNOT...@nowhere.com> wrote:
> Bruce- Hide quoted text -
>
> - Show quoted text -

Thank you all for the kind assistances, i have located the problem,
now it works well.

Harry

suchenwi

unread,
Aug 14, 2007, 3:59:24 AM8/14/07
to
On 13 Aug., 17:56, mike.hfzh...@gmail.com wrote:
> Hi there,
> I am learning TCL right now, i created a simple proc following the
> book "practical programming in TCL and TK" the code is as following:
> %proc Factorial {x} {
> set i 1;set product 1
> while {$i<=$x}
> set product [expr $product*$i]
> incr i
>
> }
> return $product
> }
>
Welcome to Tcl, Mike! You might also have a look at the "Factorial"
page on the Tclers' Wiki, http://wiki.tcl.tk/4520

0 new messages