I want to change the value of a variable within a function in a for loop. Here’s a silly example.
for k in 1:10
myfun(a,b) = a*b+k
So k is not an input, but I want to change it. I have good reasons for this, don’t worry ;).
I’m thinking I need to have the function definition as a string and then eval that string, or something like that. I don’t want to use k as a global variable, as that leads to issues.
Can anyone help?
Thanks a lot!
I was sure people would ask for the reasons behind this =P.
Basically this example is not at all how my real problem will be. The function will be called from other functions and I don’t want to risk having that unclear k in there.
My reason for doing this is just to validate some stuff in my code. So I don’t plan on keeping this in the actual code, just in a validation script.
I don’t want to modify the function or its inputs because that function is already pretty complicated in its definition (it actually belongs to a struct).
Anywho, any ideas on how to eval the function definition in the loop?
Yes, I think you got it.
I’m looping over k and it’s used in a function. I want to modify it inside that function (which I’ll redefine anyway for each value of k). So I want to redefine the function based on what’s effectively a global var (without using global vars, as they can’t be redefined).
Is this clearer?