Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Unexpected end of file in mata.


From   "William Gould, StataCorp LP" <[email protected]>
To   [email protected]
Subject   Re: st: Unexpected end of file in mata.
Date   Wed, 22 Jan 2014 11:00:12 -0600

Amadou Diallo <[email protected]> writes, 

> I am still learning (and enjoying mata !!!), but frankly the entry
> cost is big.  I don't understand why the following code is crashing.

and he shows some examples, one of which is 

    . mata
    --------------------------------- mata (type end to exit) ---
    : i = 1
    
    : if (i == 1) {
    > i
    > }
    > end
    
    unexpected end of line
    -------------------------------------------------------------

    . _

What's happening here is going to take some explaining.  Before I do
that, let me mention that Mata is designed as a programming language
that in addition, has a interactive mode.  The interactive mode mostly
works exactly as you would expect.  The exception is when you use
something that is (or at least was intended) as purely a programming
feature.  In that case, the interactive mode can surprise you.  That's
what happened here.  

So one answer is, "Why do you want to use -if- interactively, anyway?"

Amadou's answer will likely be, "because I'm trying out the features to
better understand them.  Isn't that one of the purposes of interactive
mode?"

Yes.  I certainly aprpeciate Amadou's frustration. 

Alright, so let's delve into this case.  

The basic syntax of -if- is 

                if (<exp>) {
                        <anything>
                }
                else {
                        <anything>
                }

or, more densely

                if (<exp>) <anything>
                else <anything>

where <anything> can include include braces, and must include braces 
if there is more than one statement to appear in the block. 

The -else- is optional.  Amadou typed the syntax the without an -else-. 

Mata, however, is still waiting to see the -else- or to hear from 
Amadou that the statement is complete. 

Amadou tried to tell Mata that the statement was complete by typing 
-end-:

    . mata
    --------------------------------- mata (type end to exit) ---
    : i = 1
    
    : if (i == 1) {
    > i
    > }
    > end

    unexpected end of line
    -------------------------------------------------------------

    . _

The -end- statement, however, means "exit Mata now".  So Mata exited back 
to Stata, and Mata felt obligated to mention that Amadou was leaving 
Mata in the what Mata considered the middle of a line.  

So how was Amadou supposed to tell Mata that there is no more?

He needed to type a semicolon:

    . mata
    --------------------------------- mata (type end to exit) ---
    : i = 1
    
    : if (i == 1) {
    > i
    > }
    > ;
      1

    : _

Mata is pretty relaxed about semicolons.  Mostly, it figures out 
what they need to be by itself.  In the formal programming language, 
it is nearly 100% successful in doing that.  There is one case I'll 
tell you about shortly. 

Interactively, however, this was a case where Amadou needed to say
"and there is no more", and anytime you want to say "and there is no 
more", you type a semicolon interactively. 

As I said, "Why do you want to use -if- interactively, anyway?"

In a program, Mata wouldn't have needed the semicolon, but it 
wouldn't hurt.  If I wrote the program 

        function myfcn(i)
        {
                if (i==1) {
                        i 
                        return
                }
                i = i + 1 
                "I incremented i for you"
        }

Mata would ahve figured out the semiclon after the close brace and before 
the -i = i + 1- statement.  It would ahve figured it out because 
Mata would ahve seen -i = i + 1- and said, "well, that's not -else-, so 
the -if- statement is complete."

Interactively, Mata was trying to be nice.  Watch:

    : if (i == 1) {
    > i
    > }
    >                        <- I pressed Return
    >                        <- Darn, another prompt
    >                        <- and another 
    >                        <- What's happening is that Mata is 
    >                        <- saying, "one more change to type -else-"
    >
    > ;                      <- Finally, I type semicolon -- I'm done
      1                      <- okay, says Mata, I'll execute. 

    : _

So what's the one programming case where you have to type semicolon.
It's this:

        function forexample(...)
        {
                ...
                for (i=1; mysubroutine(...)!=0; i++) ; 
                mycloseout(...)
                ...
        }

In this program, I'm calling mysubroutine() over and over again 
until mysubroutine() returns 0, and I'm couting the number of times 
I called it. 

Imagine the above code without the semicolon.

The syntax of -for- is 

              for (<exp>; <exp>; <exp>) <anything>

In this case, I want <anything> to be nothing.  I have to type a
semicolon.  if I didn't, the next line, -mycloseout(...)-, would have
been taken as the <anything> and would have been executed each time 
mysubroutine() did not return 0. 

Well, I said there was one case, but there are two.  In my defense, 
the second is really the same case as the first.  It concerns -while-.
If I didn't want to count the number of times I called mysubroutine(), 
I could have coded

        function forexample(...)
        {
                ...
                while (mysubourinte()!=0) ; 
                mycloseout(...)
                ...
        }

Note the semicolon.  Without it, mycloseout() would have been considered 
part of the body for while(). 

Well, I I said there were two cases, but there are actually three. 
In my defense, no one ever codes the third case.  It's this one:

        function lastexample(...)
        {
                ...
                if (x==1) ; 
                else y = x + 2 
                ...
        }

Note the semicolon.  I made the body of the -if- empty.  Most of us 
would code this as 

        function lastexample(...)
        {
                ...
                if (x!=1) y = x + 2
                ...
        }

So let me summarize:

    1.  Semicolon (;) may be used interactively or inside programs. 
        You use it to say, "and that's the end of the statement". 
        You are required to use it when Mata might otherwise be 
        confused and think the current statement could continue. 
        You may use whenever a statement is done. 

    2.  -end- is the instruction, "Get me out of Mata right now."

I hope this helps. 

-- Bill
[email protected]
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index