Hello, I would really appreciate your help with the following problem. I will want to learn how evaluate a variable outside of Main, and I could not find a way to do that yet.
Expected: Eval will work for #println(Core.eval(Main, Symbol(param))).
Instead: Error parameter is not defined any of the one in the list, but it is defined because I can print it no problem. println(parameter1) or pepe1. Please, see code below, thank you.
I tried eval before in Main and it works ( and it works meaning not in the function).
May expr (metaprogramming section perhaps the answer? ) I thought of some ways of generating some code like removing the “” using RegExp, and using dicitonaries to aid me, but will be much easier to learn how to evaluate inside pepito( the inner function of newfunction.jl. Thank you!
“”" A different .jl file will do this “”"
include(“newfunction.jl”)
pepito= newfunction(1)
pepito(1)
“”" first file will finish here “”"
Expected: Eval will work for #println(Core.eval(Main, Symbol(param))).
Instead: Error parameter is not defined any of the one in the list, while when I do
println(parameter1) or pepe1. I tried eval in Main( and it works meaning not in the function) and it works. So, how you evaluate something when you are not in Main. Is expr (metaprogramming section perhaps the answer? ) I thought of some ways of generating some code like removing the " " using RegExp and generating what I want and then using dicitonaries to check if things are defined etc. But, perhaps either Expr will help or maybe Module.
module amodule
I tried include(“newfunction,jl”)
end
Then in the function I will use eval(Main.amodule, Symbol(param)) # Still does not work ?
Again thank you!!!
“”" THIS IS MY FUNCTION “”" ( newfunction.jl)
function newfunction(input)
parameter1 = input
parameter2 = input + 1
parameter3 = input + 3
“”" parameters “”"
parameters= [“pepe1”, “pepe2”, “pepe3”]
println(parameters)
function pepito2(t)
println(“Starting to print parameter from parent”)
println(parameter1)
println(parameter2)
println(parameter3)
println(“Done”)
pepe1 = parameter1 + 1
pepe2 = parameter2 + 2
pepe3 = parameter3 + 2
z1= pepe1 + pepe2 + pepe3
println(“Now we are printing pepe1”)
println(pepe1)
println(“Now we are printing pepe2”)
println(pepe2)
println(“Now we are printing pepe3”)
println(pepe3)
newdic= Dict()
for param in parameters
if @isdefined(param)==true
println(param, "is defined")
#println(Core.eval(Main, Symbol(param))) I tried both.
println(eval(Symbol(param)))
else
println("NotDefined")
end
end
“”"
if t!=0
for param in parameters
if @isdefined(param)==true
println(param, "is defined")
println(eval(Symbol(param)))
else
println(param)
println("NotDefined")
end
end
end
“”"
return z1
end
return pepito2
end
“”“A different .jl file will do this “””
include(“newfunction.jl”)
pepito= newfunction(1)
pepito(1)
Expected: Eval will work for #println(Core.eval(Main, Symbol(param))).
Instead: Error parameter is not defined any of the one in the list, while when I do
println(parameter1) or pepe1. I tried eval in Main( and it works meaning not in the function) and it works. So, how you evaluate something when you are not in Main. Is expr (metaprogramming section perhaps the answer? ) I thought of some ways of generating some code like removing the “” and using dicitonaries to aid me.
“”" THIS IS MY FUNCTION “”" ( newfunction.jl)
function newfunction(input)
parameter1 = input
parameter2 = input + 1
parameter3 = input + 3
“”" parameters “”"
parameters= [“pepe1”, “pepe2”, “pepe3”]
println(parameters)
function pepito2(t)
println(“Starting to print parameter from parent”)
println(parameter1)
println(parameter2)
println(parameter3)
println(“Done”)
pepe1 = parameter1 + 1
pepe2 = parameter2 + 2
pepe3 = parameter3 + 2
z1= pepe1 + pepe2 + pepe3
println(“Now we are printing pepe1”)
println(pepe1)
println(“Now we are printing pepe2”)
println(pepe2)
println(“Now we are printing pepe3”)
println(pepe3)
newdic= Dict()
for param in parameters
if @isdefined(param)==true
println(param, "is defined")
#println(Core.eval(Main, Symbol(param))) I tried both.
println(eval(Symbol(param)))
else
println("NotDefined")
end
end
“”"
if t!=0
for param in parameters
if @isdefined(param)==true
println(param, "is defined")
println(eval(Symbol(param)))
else
println(param)
println("NotDefined")
end
end
end
“”"
return z1
end
return pepito2
end
"""A different .jl file will do this """
include("newfunction.jl")
pepito= newfunction(1)
pepito(1)
Expected: Eval will work for #println(Core.eval(Main, Symbol(param))).
Instead: Error parameter is not defined any of the one in the list, while when I do
println(parameter1) or pepe1. I tried eval in Main( and it works meaning not in the function) and it works. So, how you evaluate something when you are not in Main. Is expr (metaprogramming section perhaps the answer? ) I thought of some ways of generating some code like removing the "" and using dicitonaries to aid me.
""" THIS IS MY FUNCTION """ ( newfunction.jl)
function newfunction(input)
parameter1 = input
parameter2 = input + 1
parameter3 = input + 3
""" parameters """
parameters= ["pepe1", "pepe2", "pepe3"]
println(parameters)
function pepito2(t)
println("Starting to print parameter from parent")
println(parameter1)
println(parameter2)
println(parameter3)
println("Done")
pepe1 = parameter1 + 1
pepe2 = parameter2 + 2
pepe3 = parameter3 + 2
z1= pepe1 + pepe2 + pepe3
println("Now we are printing pepe1")
println(pepe1)
println("Now we are printing pepe2")
println(pepe2)
println("Now we are printing pepe3")
println(pepe3)
newdic= Dict()
for param in parameters
if @isdefined(param)==true
println(param, "is defined")
#println(Core.eval(Main, Symbol(param))) I tried both.
println(eval(Symbol(param)))
else
println("NotDefined")
end
end
"""
if t!=0
for param in parameters
if @isdefined(param)==true
println(param, "is defined")
println(eval(Symbol(param)))
else
println(param)
println("NotDefined")
end
end
end
"""
return z1
end
return pepito2
end
Thank you once more again! I look forward to your response.
FYI I think I said that I did try :
module amodule
include("newfunction.jl")
end
and tried eval(Main,Symbol(param)). But, I forgot to mention that then I tried to put a new module from the beggining of pepito (inner function) to end , but I got an error that module needs to be at top, so that definitely did not work for me, thanks!
Christian