100 lines
2.4 KiB
Julia
100 lines
2.4 KiB
Julia
""" prompt
|
|
I want to have my main function loop running and execute a user-defined script: usermodule_1.jl,
|
|
usermodule _2.jl and so on.
|
|
1). Write a main.jl with main function loop to execute user-defined scripts sequentially.
|
|
2). Store each script result in respective variable.
|
|
3). make sure no global variables contamination Give me a complete example.
|
|
"""
|
|
|
|
# ---------------------------------------------- 100 --------------------------------------------- #
|
|
|
|
"""To achieve your goal of running user-defined scripts sequentially within a main function loop in Julia, storing each script's result in a respective variable, and ensuring no global variable contamination, you can follow the example below:
|
|
Create a main.jl script with a main function loop to execute user-defined scripts sequentially:"""
|
|
|
|
# -------------------------------------------- main.jl ------------------------------------------- #
|
|
function main()
|
|
script_results = Dict{String, Any}()
|
|
|
|
for i in 1:2
|
|
script_name = "usermodule_$i.jl"
|
|
include(script_name)
|
|
|
|
# Assuming each user-defined script has a function named 'run_script' that returns a result
|
|
result = run_script()
|
|
|
|
script_results[script_name] = result
|
|
end
|
|
|
|
return script_results
|
|
end
|
|
|
|
results = main()
|
|
|
|
|
|
# ---------------------------------------- usermodule_1.jl --------------------------------------- #
|
|
module UserModule1
|
|
|
|
export run_script
|
|
|
|
function run_script()
|
|
# Your script logic here
|
|
result = "Result from UserModule1"
|
|
return result
|
|
end
|
|
|
|
end
|
|
|
|
# ---------------------------------------- usermodule_2.jl --------------------------------------- #
|
|
module UserModule2
|
|
|
|
export run_script
|
|
|
|
function run_script()
|
|
# Your script logic here
|
|
result = "Result from UserModule2"
|
|
return result
|
|
end
|
|
|
|
end
|
|
|
|
""" Ensure no global variable contamination by encapsulating each script's logic within a module and exporting only the necessary functions.
|
|
|
|
When you run main.jl, it will sequentially execute usermodule_1.jl and usermodule_2.jl, store the results in a dictionary script_results, and return it. Each script's logic is encapsulated within its module to prevent global variable contamination.
|
|
|
|
This approach allows you to run user-defined scripts sequentially, store their results, and maintain a clean and modular structure in your Julia code.
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|