Can't run for-loop

Tend          = 115;
Period_all    = zeros(Tend, 1);
timeidx       = 1950;


  for tt = 1:Tend

      Period_all[tt] = timeidx;
      timeidx        = timeidx + 0.25;

  end

ERROR: syntax: incomplete: premature end of input
is printed after the execution of the 4. line (for)

ERROR: UndefVarError: tt not defined
after the 5. line and

ERROR: syntax: unexpected "end"
after the end command in the last line.

I can run the code iteration by iteration by setting tt to 1, 2,…Tend sequentially. I use Atom. What am I missing?

Either you include the code from a file or you have to use syntax like for i = 1:10; println(i); end.

Your code is legal and, when I paste it in my REPL, there are no errors. Can you provide more details about what, exactly, you did that led to these error messages?

I’d guess he tried to execute the code line by line.

Sure. Maybe I am running the code in the wrong way.

I have a script “test.jl” that includes only the lines given above. I select all of it, and copy-paste it into the command line. That gives me the reported errors.

I tried line by line and copying and pasting the entire code.

By “command line,” do you mean the Julia REPL?

Yes.

I think to make progress we’ll have to have a screen shot showing the pasted code and the error messages.

1 Like

Try replacing for tt = with for tt in.

Same outcome - I had tried this before.

You are running a rather old version of Julia there. Try a newer one and I suspect things will be smoother.

1 Like

To elaborate on what’s happening here, that REPL is trying to evaluate incomplete but so-far-valid input. The normal terminal REPL waits for a complete input. It might have something to do with “paste mode” behaving strangely or maybe whatever very old setup you’re using was just broken this way, hard to say or remember. In any case there’s no reason to be using Julia 1.0 at this point, use a newer Julia and an up-to-date IDE.

2 Likes

Notice that even in the lines which do work, one newline in your editor is becoming two newlines in the REPL. This could be some quirk of OS line-endings and copy-paste?

The REPL allows one completely empty line, but on the second one, it gives up and presumes this was your complete input:

julia> for tt = 1:Tend    # with one empty line, as above

             Period_all[tt] = timeidx;
             timeidx        = timeidx + 0.25;

         end

julia> for tt = 1:Tend   # with two empty lines -- fails


ERROR: syntax: incomplete: premature end of input
Stacktrace:
 [1] top-level scope
   @ none:1
2 Likes

Ok thanks, will try

Yes, realized this too.

Aside from the above suggestions, if you’re willing to use VSCode instead you can add the Run extension and will then be able to right click inside your script and click Run as long as the Julia path is defined.

2 Likes

Maybe your code have strange blank characters.
Or something is corrupted in your Julia installation.