[comp.sys.mac.hypercard] if-statement problem

langford@reed.UUCP (Chris Langford) (10/07/88)

Hi,
I have been writing in hyperTalk for a while, and there is a problem I
have run into several times.  When I try to run scripts that contain
if statements with this format:

	on mouseUp
  	  repeat with i = 1 to the number of cards
    	    if field 1 of cd i contains "RTFM" then
      	      foobar
    	    end if
  	  end repeat
	end mouseUp

	-- where foobar is any command or set of commands

I get an error message that says "No such card".  The problem is fixed if 
I write the script:

	on mouseUp
    	  repeat with i = 1 to the number of cards
	    get field 1 of cd i
            if it contains "RTFM" then
	      foobar
	    end if
	  end repeat
	end mouseUp

or like this:

        on mouseUp
          repeat with i = 1 to the number of cards
            go cd i
            if field 1 contains "RTFM" then
              foobar
            end if
          end repeat
        end mouseUp

The second correction takes longer to run than the first, but I think
the original script would run even faster, if it worked (one less line).
Am I missing something completely, or is this a problem with HC?
Has anyone else run into this problem?

Thanks for any help.
-- 
Chris Langford                 | 
tektronix!reed!langford        |         "And to everyone else out there,
langford@reed.bitnet           | 	  the secret is to bang the rocks
                               |          together, guys." 

Scot.Kamins@f555.n125.z1.FIDONET.ORG (Scot Kamins) (10/09/88)

The solution to your problem is to put the field reference within parentheses:
 
    if (field 1 of cd i) contains "RTFM" then
 
Hypertalk gets confused when you give it an expression to evaluate as a field address if that expression is anyplace but at the end of a 
line. So if the line were something like
     else put foo into field 1 of cd 1
it would have no problem with it.
The solution I suggest will work every time.


--  
Scot Kamins - via FidoNet node 1:125/406
UUCP: ...!sun!hoptoad!fidogate!555!Scot.Kamins
ARPA: Scot.Kamins@f555.n125.z1.FIDONET.ORG

tgl@zog.cs.cmu.edu (Tom Lane) (10/10/88)

In article <10537@reed.UUCP>, langford@reed.UUCP (Chris Langford) writes:
> [...] When I try to run scripts that contain if statements with this format:
> 
>     	    if field 1 of cd i contains "RTFM" then
> 
> I get an error message that says "No such card".  The problem is fixed if 
> I write the script:
> 
> 	    get field 1 of cd i
>           if it contains "RTFM" then
> 
> or like this:
> 
>             go cd i
>             if field 1 contains "RTFM" then
> 

The problem goes away if you add some parentheses:

    	    if (field 1 of cd i) contains "RTFM" then

Evidently, when you write the command without parentheses, HC parses it as

    	    if field 1 of cd (i contains "RTFM") then

hence the error "no such card" (unless you have a card named "false").

It would seem that after "card" HC looks for a general expression, whereas
after "field" it looks for a more restricted expression---since Chris's
last example is not parsed as

            if field (1 contains "RTFM") then


<flame on>

This sort of undocumented inconsistency is the reason why people have been
complaining since day one about HyperCard's weird syntax.  Has the HC team
ever heard of formal syntax specifications (e.g., BNF notation)?  If so, why
do they still refuse to publish one?  There is nothing about the relative
precedence of arithmetic operators and chunk-expression operators in the
HyperCard Script Language Guide, and certainly nothing to suggest that
arithmetic expressions in different parts of a chunk expression will be
parsed differently.

I get the impression that Apple believes that formal specifications would
scare away potential scripters who don't understand formal methods.  I beg
to differ.  Failing to publish a formal specification for HyperTalk syntax is
not going to make scripting any easier; it just creates more room for
problems of the kind shown above.  (A truly scary thought is that Apple
doesn't *have* a formal specification, and will be as surprised as the
rest of us by problems of this ilk.)

<flame off>

I don't want to offend anybody at Apple, but I do think it's high time
to publish a *complete* specification of the language.  Failure to do
so suggests that they do not understand their product any better than
the rest of us.

-- 
				tom lane
Internet: tgl@zog.cs.cmu.edu
UUCP: <your favorite internet/arpanet gateway>!zog.cs.cmu.edu!tgl
BITNET: tgl%zog.cs.cmu.edu@cmuccvma

tgl@zog.cs.cmu.edu (Tom Lane) (10/14/88)

Here's some more info about the problem Chris Langford posed, namely that
    	    if field 1 of cd i contains "RTFM" then
fails while
            if field 1 contains "RTFM" then
does not.

It seems that following "card", "card id", "background", or "background id"
(or abbreviations of same), HyperCard expects to find an arbitrary expression.
This expression is everything up to the next "of", "in", "then", or similar
keyword, end-of-line, or right parenthesis that matches a prior left
parenthesis.  Hence
    	    if (field 1 of cd i) contains "RTFM" then
works.

The same rule applies for expressions in a chunk-selector (i.e., following
"char", "word", "item", or "line").

BUT: following "button", "button id", "field", or "field id", HyperCard
expects to find a "factor" as defined on p. 49 of the Script Language Guide.
This is:
	* a simple source of value (constant or variable name);
	* a parenthesized expression;
	* a factor preceded by "-" or "not".

This explains why Chris's second example works: the "contains ..." is not
part of the factor which is the field number.  If you try something like
	put the name of field 1+1
you get a complaint, not the name of field 2.

A factor is what is taken to be the argument of a parenthesis-less function;
for instance, "the sqrt of 4+12" means "sqrt(4)+12", not "sqrt(4+12)".


While I can think of arguments in favor of using either general expressions
or factors as the value selecting an object, I find it hard to see why
fields and buttons should be treated differently from cards and backgrounds
in this regard.  I also don't find anything about this topic in the Script
Language Guide.  (I derived the above rules by experiment.)  Anybody from
Apple care to comment?

-- 
				tom lane
Internet: tgl@zog.cs.cmu.edu
UUCP: <your favorite internet/arpanet gateway>!zog.cs.cmu.edu!tgl
BITNET: tgl%zog.cs.cmu.edu@cmuccvma