Julia throws undefined error when defining variable


oo92

I have i = 1a simple while loop that is used as an index .

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

but I get this error:

UndefVarError: i not defined

top-level scope@Local: 23

I even made my i global as suggested on some similar questions on SO, but the error persists. I'm running it on Pluto.jl, so it might be an environment issue.

Benoit Pasquier

First, note that if you're using Julia v1.5+, then you don't need to do i(the current stable v1.5.4 example below) globally:

julia> i = 1
1

julia> while i < 5
           println(i)
           i += 1     # <----- this works in Julia v1.5+
       end
1
2
3
4

However, it seems you are using old Julia V1.4, in which case I think Logan Kilpatrick gave you the answer: you need to do ia global scope from the loop inside . As Logan mentioned, try adding the increment's location , like this example from the function's documentation :whileglobaliwhile

julia> i = 1 ;

julia> while i < 5
           println(i)
           global i += 1     # <------------ Try this!
       end
1
2
3
4

Also note that you don't need to specify that it's a global if your whileloop is inside a function like

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

Related


Undefined variable error when defining variable

Julien Vincent When defining a variable, I get an "undefined variable" error. bellow is my code <?php $imagepath = $_SESSION['path']; require_once('class-db.php'); if ( !class_exists('INSERT') ) { class INSERT { public function post($postdata) {

Undefined variable error when defining variable

Julien Vincent When defining a variable, I get an "undefined variable" error. bellow is my code <?php $imagepath = $_SESSION['path']; require_once('class-db.php'); if ( !class_exists('INSERT') ) { class INSERT { public function post($postdata) {

Generic throws an error when defining a method in Java

MetallicPriest: I am learning Generics in Java. For this, I tried a simple LinkedList like this. class Node { private int age; private String name; private Node next; public Node(int age, String name) { this.age = age; this.name = name; th

Generic throws an error when defining a method in Java

MetallicPriest: I am learning Generics in Java. For this, I tried a simple LinkedList like this. class Node { private int age; private String name; private Node next; public Node(int age, String name) { this.age = age; this.name = name; th

Julia error - constructor not found when defining

bar Copied it into a jupyter notebook cell, but it doesn't run, and the message doesn't really help. Everything looks good. mutable struct CircularArray{T} <: AbstractArray{T,1} data::Array{T,1} first::Int CircularArray{T}(length::Int) where {T} =

Julia error - constructor not found when defining

bar Copied it into a jupyter notebook cell, but it doesn't run, and the message doesn't really help. Everything looks good. mutable struct CircularArray{T} <: AbstractArray{T,1} data::Array{T,1} first::Int CircularArray{T}(length::Int) where {T} =

Strange error when defining variable

Duy So I'm trying to use the Lua C Api on the game, but I'm getting some errors when defining the variables in the .cpp file I have a library called CLua.h that defines everything I use in the following code and it compiles fine. (I won't share it because it's

underscore template throws variable undefined error

ra170 I watched some videos about trunk js. Here's an example straight from the video. It's from 2012, so I think the trunk rules/libraries have changed, but I don't know why this doesn't work for the time being. In the video, the guy demonstrates it working i

Seemingly defined javascript variable throws undefined error

manalone Given the following code, I'm not sure why I can retrieve longAndObscureVariableName but not anotherLongObscureVariableName. Can you explain, and show how to make anotherLongObscureVariableName accessible to jsFileNumberTwo.js? The header of the HTML

Why code throws error 'variable' is undefined

X10nD my code: def divide(x,y): div = divmod(x,y) return div query = input("Enter numbers separated with a comma to divide: ") divide(int(query.split(',')[0]),int(query.split(',')[1])) print(div) Why is the error thrown: NameError: name 'div' is not

underscore template throws variable undefined error

ra170 I watched some videos about trunk js. Here's an example straight from the video. It's from 2012, so I think the trunk rules/libraries have changed, but I don't know why this doesn't work for the time being. In the video, the guy demonstrates it working i

Seemingly defined javascript variable throws undefined error

manalone Given the following code, I'm not sure why I can retrieve longAndObscureVariableName but not anotherLongObscureVariableName. Can you explain, and show how to make anotherLongObscureVariableName accessible to jsFileNumberTwo.js? The header of the HTML

underscore template throws variable undefined error

ra170 I watched some videos about trunk js. Here's an example straight from the video. It's from 2012, so I think the trunk rules/libraries have changed, but I don't know why this doesn't work for the time being. In the video, the guy demonstrates it working i

Seemingly defined javascript variable throws undefined error

manalone Given the following code, I'm not sure why I can retrieve longAndObscureVariableName but not anotherLongObscureVariableName. Can you explain, and show how to make anotherLongObscureVariableName accessible to jsFileNumberTwo.js? The header of the HTML

DolphinDB throws exception when reassigning undefined variable

ly In DolphinDB, I define a variable, called the undeffunction that undefines the function, and reassign a vector to it. But my code throws an exception that I can't understand. To simplify things, I'll provide a minimal example that throws an exception: a = 0

DolphinDB throws exception when reassigning undefined variable

ly In DolphinDB, I define a variable, called the undeffunction that undefines the function, and reassign a vector to it. But my code throws an exception that I can't understand. To simplify things, I'll provide a minimal example that throws an exception: a = 0