Setting Variables from a Parameters Text File

Is it possible to do this and how would I do it?

I have a file populated with variable name, variable type, and variable value.

name,type,value
var1,Int64,44
var2,Float64,1.5
var3,String,Hello World

I want to use this file to dynamically create variables in Julia.

I am assuming that loading the data into variables is not an issue.

Let’s say we have loaded the equivalent of

vName = :var1; vType = :Float64; vValue = 11;

Then

julia> @eval $vName = $vType($vValue);

julia> var1

11.0

Thank you! I was not aware of the @eval function.

Note that this is only possible in global scope.

1 Like

That’s a good call. Thank you for clarifying. Fortunately, I plan to use this for setting constants at the global scope: @eval const $vName = $vType($vValue);.

I would just use a standard format like JSON and read it into a dictionary with JSON.jl. Much more flexible that parsing your own format and calling eval to define global variables.

3 Likes