定义变量时,Julia抛出未定义错误

定义变量时,Julia抛出未定义错误,julia,pluto.jl,undefvarerror,Julia,Pluto.jl,Undefvarerror,我有一个简单的while循环,它使用I=1作为索引 global i = 1 n = rows while i <= n if prod(isa.(collect((y)[i,:]),Number))==0 delete!(y,i) x_axis = x_axis[1:end .!= i] n -= 1 end i += 1 end

我有一个简单的while循环,它使用
I=1
作为索引

global i = 1 
    
    n = rows
    while i <= n
        if prod(isa.(collect((y)[i,:]),Number))==0
            delete!(y,i)
            x_axis = x_axis[1:end .!= i]
            n -= 1
        end
        i += 1
    end

我甚至根据关于SO的一些类似问题的建议制定了我的I global,但错误依然存在。我在Pluto.jl上运行这个程序,所以可能是环境问题。

首先,请注意,如果您使用Julia v1.5+,则不需要将
I
设置为全局(当前稳定版本v1.5.4的示例如下):

还请注意,如果
while
循环位于函数内部,则无需指定它是全局循环,如中所示

julia> function foo(istart)
           i = istart
           while i < 5
               println(i)
               i += 1     # <-- 'global' not needed inside a function!
           end
       end
foo (generic function with 1 method)

julia> foo(1)
1
2
3
4
julia>函数foo(istart)
i=istart
当我<5岁时
println(i)
i+=1#foo(1)
1.
2.
3.
4.
您已命中“不明确的软范围案例”

简而言之:局部变量在(软)局部作用域中的赋值取决于代码是否在“REPL上下文”中 或者不是

例如,对于“REPL-context”,我指的是REPL和在本例中作为REPL的所有环境 朱皮特:

julia>i=0
朱莉娅>当我<3
i+=1
@信息一
结束
[信息:1]
[信息:2]
[信息:3]
相反,来自非交互式上下文(如file、eval和Pluto)的代码如下所示:

julia> code = """
       i = 0
       while i < 3
         i += 1
         @info i
       end
       """

julia> include_string(Main, code)
┌ Warning: Assignment to `i` in soft scope is ambiguous because a global variable by the same name exists: `i` will be treated as a new local. Disambiguate by using `local i` to suppress this warning or `global i` to assign to the existing global variable.
└ @ string:3
ERROR: LoadError: UndefVarError: i not defined
julia>code=”“”
i=0
当我<3
i+=1
@信息一
结束
"""
julia>包含字符串(主,代码)
┌ 警告:在软作用域中对'i'的赋值不明确,因为存在同名的全局变量:'i'将被视为新的局部变量。通过使用'local i'抑制此警告或使用'global i'分配给现有全局变量来消除歧义。
└ @ 字符串:3
错误:LoadError:UndevarError:i未定义
所有这些都是为了确保REPL使用的便利性和避免大规模使用julia带来不必要的副作用而设计的

全部细节


要解决此问题,您可以按照建议使用
global
,或者将代码封装在函数中。

Pluto将单元格隐式包装到函数中,请参阅,因此不需要在函数中使用
global
注释或显式包装

将以下内容放入冥王星细胞对我有用:

begin
    i = 1
    n = 100
    while i<=n
        if i % 2 == 0
            n -= 1
        end
        i += 1
    end
end

我想如果你在你的函数接近尾声时做
global I+=1
,这会起作用。请随意通过这里联系参考:冥王星是这里的一个特例,请参阅下面我的答案。
julia> function foo(istart)
           i = istart
           while i < 5
               println(i)
               i += 1     # <-- 'global' not needed inside a function!
           end
       end
foo (generic function with 1 method)

julia> foo(1)
1
2
3
4
julia> i = 0
julia> while i < 3
         i += 1
         @info i
       end
[ Info: 1
[ Info: 2
[ Info: 3
julia> code = """
       i = 0
       while i < 3
         i += 1
         @info i
       end
       """

julia> include_string(Main, code)
┌ Warning: Assignment to `i` in soft scope is ambiguous because a global variable by the same name exists: `i` will be treated as a new local. Disambiguate by using `local i` to suppress this warning or `global i` to assign to the existing global variable.
└ @ string:3
ERROR: LoadError: UndefVarError: i not defined
begin
    i = 1
    n = 100
    while i<=n
        if i % 2 == 0
            n -= 1
        end
        i += 1
    end
end
begin
    i = 1
    n = 100
    while i<=n
        if i % 2 == 0
            n -= 1
        end
        @show i += 1
    end
end
UndefVarError: i not defined

top-level scope@Local: 5[inlined]
top-level scope@none:0