Hello,
I want to generate lines of code automatically, using a list of field names as input.
The following code doesn’t throw an error, but it also doesn’t produce the desired result, because the eval function needs an expression as argument and not a string.
How can I make it work?
#!/usr/bin/env julia
using CSV
# create dummy csv file for testing
csv =
"""
time, pos_x, pos_y, pos_z
0.0, 0.0, 0.0, 0.0
0.1, 1.0, 2.0, 3.0
0.2, 1.5, 3.0, 4.0
"""
f = open("test.csv", "w")
write(f, csv)
close(f)
# read the csv file into a DataFrame
data = CSV.read("test.csv")
# dummy function for testing
function resample(a,b)
return b
end
# define the fields for code generation
fields = "pos_x","pos_y","pos_z"
# preparations
const slice = 1:2
const times = data[:time]
const time_slice = times[slice]
# create constant, global arrays in a loop
for field in fields
line = ("const $field = resample(time_slice, data[:$field][slice])")
println(line)
eval(line)
end
#=
# The following code should be generated and executed
const pos_x = resample(time_slice, data[:pos_x][slice])
const pos_y = resample(time_slice, data[:pos_y][slice])
const pos_z = resample(time_slice, data[:pos_z][slice])
=#
Any hints appreciated!