Hey! Have you read this page? Scope of Variables · The Julia Language
It’s usually a good idea to put the code in a function rather than operating in global scope. If you want to modify the loop variable, are you sure you want to use a for loop? You may use the keyword outer to have the loop variable refer to an outer variable, in which case you must initialize it before the loop.
There is! For now, use jupyter as your interface for “scripting” as you would the matlab ide. Then when you want to define .jl files and use Atom, put anything with loops in a function instead of a top-level script. If you do this, you will never run into this problem again.
You keep suggesting this in all scope-related threads, but it is not always the solution. In this case for example, the loop variable itself (i_exh) is never available outside of the loop and that has nothing to do with “softscope” as enabled in IJulia, Julia REPL 1.5.
Others told a matlab user to read up on the complicated scoping rules. For beginners coming from matlab they are much better off using jupyter, as it is far more intuitive. I didn’t want a new user to be scared off. Global variables are very different in matlab.
That said, for this it looks like you are 100 percent right that it wouldn’t fix it immediately, but it is wrong to say that Jupyter is orthogonal, as it makes the solution easier for this type of user.
For a matlab user on jupyter, the best solution is to create a variable outside of the loop, then modify it in the loop, and it works exactly as they would expect (and without a global keyword) . So, @Ujku_KU my suggestion is to look at this code
val = 0
for i = 1:10
val = i
if i > 5
break
end
end
println(val)
Which works great in Jupyter. Also not that if I put it in a function
function f()
val = 0
for i = 1:10
val = i
if i > 5
break
end
end
println(val)
end
f()
It works great at well. You would want to make sure it was in a function when using Atom or putting it in a .jl file.
So to summarize, and correct my original statement given what @fredrikekre pointed out.
A key difference between matlab and Julia is that loops have their own scope, so any variables created in them (including the indices) are not available outside of the scope. This has nothing to do with global variables,and I think you will find that it is a much better approach than matlab.
So you need to have the stuff in the loop modify something that was available outside of it. If you use jupyter, you will not need to think about the difference between doing that within a function and within a script. It will be intuitive
If you want to learn to use a new language, isn’t learning the scoping rules a rather good idea? I think it’s a mistake to think that one can just switch to a new language without having to learn anything new. Reading the julia manual is a rather small investment in time that pays off quite soon. Also, using jupyter is a very different experience to using an IDE and it might just as well cause additional headache alongside patching up some scoping issue.