Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia 1.1 x=x+1 in for loop returned error

Tags:

julia

I'm using Julia 1.1 and I tried to use for loop do the following simple things:

i_index=1;
for index in (1:100)
    i_index=i_index+1;
end

However, I got an error saying:

ERROR: UndefVarError: i_index not defined

I have tried several times and variations, but they all failed to work. Is this a bug? or why Julia can't do this simple iterative addition?

like image 343
ShoutOutAndCalculate Avatar asked Nov 07 '22 19:11

ShoutOutAndCalculate


1 Answers

In the REPL:

i_index=1;
for index in (1:100)
    global i_index;
    i_index=i_index+1;
end

This is because of variable scope, see in Julia documentation. Note that the examples there pertain to the REPL.

like image 122
Inon Peled Avatar answered Nov 29 '22 12:11

Inon Peled