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

else is an invalid command?

902 views
Skip to first unread message

zhong yu

unread,
May 24, 1996, 3:00:00 AM5/24/96
to

Hi all:
I just installed tcl7.5 and tk4.1. When I wrote a script do some
file processing, it tells me that "invalid command name 'else' ", "elseif"
doesn't work either. How could this be happening? I mean for every
language there exist, "if" always team up with "else" and "elseif". I
looked up Ousterhout's book and "else" is there. Is this because they
changed the sytnax in tk4.1? Or something is wrong with my code?

here is the code:

_________________________
proc match {} {
set temp apple
set f [open /usr/fruits.txt r+]
while {[gets $f line] != -1} {
if {![regexp $temp [lindex $line 0]]} {
puts stdout "does match"
}
else {puts stdout "doesn't match"}
}
close $f
}
_______________________
if you run this proc in either tclsh or wish, you will get
"invalid command name "else"".

Any ideas? Weird, isn't it?

Thanks.

Clark Zhong
One week into tcl\tk

Michael Salmon

unread,
May 25, 1996, 3:00:00 AM5/25/96
to

I was also surprised the first time that it happened to me. What you need to remember is that
a tcl command consists of a command name followed by zero or more parameters on a SINGLE line,
if in tcl is a command with at least two parameters, a boolean value and a block of code (in a
list) optionally you can follow with elseif triplets and a final else pair. All of this must
be on a single line. New lines inside of lists are ignored as are new lines escaped by \, so
you need to change your code to } else { or }\

--
© 1995,1996 Michael Salmon
All opinions expressed in this article remain the property of
Michael Salmon. Permission is hereby granted for use in
followup articles, FAQ's and digests.

Joe Moss

unread,
May 25, 1996, 3:00:00 AM5/25/96
to

y-z...@ux7.cso.uiuc.edu (zhong yu) writes:

> I just installed tcl7.5 and tk4.1. When I wrote a script do some
>file processing, it tells me that "invalid command name 'else' ", "elseif"
>doesn't work either. How could this be happening? I mean for every
>language there exist, "if" always team up with "else" and "elseif". I

That is the problem. You are expecting Tcl to act like other languages.
Tcl is much, much simpler than many programming languages.

>looked up Ousterhout's book and "else" is there. Is this because they
>changed the sytnax in tk4.1? Or something is wrong with my code?

What little syntax there is in Tcl didn't change.

>here is the code:

>_________________________
>proc match {} {
>set temp apple
>set f [open /usr/fruits.txt r+]
> while {[gets $f line] != -1} {
> if {![regexp $temp [lindex $line 0]]} {
> puts stdout "does match"
> }
> else {puts stdout "doesn't match"}
> }
> close $f
>}
>_______________________
> if you run this proc in either tclsh or wish, you will get
>"invalid command name "else"".

Which is the correct behaviour, since there is no "else" command.

> Any ideas? Weird, isn't it?

No, it's typical. That is, typical of the results that occur when
people have used other languages and then try to use Tcl by applying
the principles of syntax of those other languages to Tcl. It just
doesn't work.

The easy answer to your problem is to just rearrange the placement of
braces, like this:

proc match {} {
set temp apple
set f [open /usr/fruits.txt r+]
while {[gets $f line] != -1} {
if {![regexp $temp [lindex $line 0]]} {
puts stdout "does match"
} else {
puts stdout "doesn't match"
}
}
close $f

However, I'm trying to point out that there is a more fundamental
problem to deal with. You need to understand the rules, the simplicity,
of Tcl. Read the description of the language in the Tcl man page.
It is not an overview, it's the whole thing, there ain't no more to
the language.

In this particular case you are expecting an if-then-else type
construct, because that's common in programming languages, but Tcl
has no such construct. "if" has no special meaning to the parser.
It's just a command like any other. Everything is just

command arg1 arg2 ....

The parser doesn't even know if the command wants arguments or not.
It just passes whatever arguments are given (if any). It's up to the
command to decide what to do with it's args.

The "if" command just happens to always use it's first argument as
a conditional expression and to evaluate a later arg depending on the
value of the conditional.

I hope this didn't too harsh. I'm just trying to save you some
trouble in the long run. A large percentage of the postings in
this group are from people who are having troubles with Tcl, because
they fail to understand just how simple-minded the Tcl parser really is.


--
set Email "j...@morton.rain.com (Joe Moss)";set URL http://psg.com/~joem/
======================================================================
"Folk, if you are not checking the Frequently Asked Questions documents
before you ask questions, you are taking longer to resolve problems,
and annoying more folk, than necessary." -- Larry W. Virden

0 new messages