Monika Galińska
Monika Galińska

Reputation: 201

How to return variable in Julia error variable is not defined

Hello i have probelm i trying return my defined variable but i get a error variable is not defined

type Family
        name:: AbstractString
        value:: Int
        left:: Nullable{Family}
        right:: Nullable{HuffmanNodeA}  
        Family(name:: AbstractString, value::Int ) = new(name, value , Nullable{Family}(), Nullable{Family}())
        end

A = [Family("Julia", 24), ...]

function minimalnia() 
global Family family
min = A[1].value
for i in A
if(i.value < min )
mininmalny = i.value
family = i 
end 
end
println(i)
return family
end 

I get error "family is not defined" , why i get this error, how to correct my code?

Upvotes: 0

Views: 414

Answers (1)

David P. Sanders
David P. Sanders

Reputation: 5325

There are many errors in your code. You should start from something simple and build it up, making sure that it works at each step.

Here is a working, simplified version for you to compare with to see what you were doing wrong:

type Family
    name::UTF8String
    value::Int
end


function minimal(families::Vector{Family}) 
    minfamily = Nullable{Family}()

    minval = families[1].value  # will not work if families is empty
    for f in families

        if f.value < minval
            minval = f.value
            minfamily = f 
        end 
    end

    return minfamily
end 

F1 = [Family("Julia", 24)]
@show minimal(F1)

F2 = [F1; Family("Yullia", 23)]
@show minimal(F2)

However, since you are just looking for the minimum over an array, there is a simpler way to do this with Julia.

Upvotes: 2

Related Questions