I am trying to read parameters from a .csv file for which the name of parameter and its values are given in the .csv file.
For that I am trying to simply evaluate a simple expression involving putting the value of the parameter in a struct, but I get error.
I will be delighted if you could provide me a solution to this simple problem?
There seem to be a bug since you are correct the code works well in REPL but fails in a function.
Yes I have the definition of hydro and the execution of eval in a different modules
Generally it is advisable to avoid using eval. From your example I do not see why you need eval at all as directly using hydro.θs[1] = 0.5 without using eval would work.
If your use case requires names that you will only know after you load the CSV I would suggest storing the data in either a Dict or a DataFrame depending on your use case. Both of these allow you to index by name.
Thanks for your great help and your time. Unfortunately when I introduced your recommendation into the complex program, I was not able to make it work, I am still getting the same error. I think that I would need more clarity on the meaning of Main.T ?
T is just the name of the module that KDr2 is using to hold onto your global variable. All variables you want to change/assign/update/touch with eval would need to be globals there — and you need to ensure that you’re explicitly evaling into that module.
But I really think you’ll want to change your angle of attack here — using eval like this isn’t very sustainable or maintainable. The other alternatives proposed here will likely make your life much easier. Another option would be a file format like TOML that can work nicely for what I think you’re trying to do.
Is that you can perform Metaprogramming by avoiding eval. I can now read the symbol directly from a csv file and put it automatically into the structure hydro
You are correct, it is the downside of the method.
If you have a method to create struct on the fly that will be great. My challenge is that some parameters are vectors.
My understanding is that one of the limitation of Dict is that one cannot use Vectors. “θs” is a parameter describing the maximum saturated soil moisture of a layer in a soil. I have many soil layers, so my understanding is that Dict might not be the best solution for using Vectors, but I might be wrong. For e.g. this may not be possible: