El miércoles, 11 de febrero de 2015, 14:49:53 (UTC-6), tshort escribió: > > You probably don't want `eval` unless there's no other way. It's hard to > tell how you want to use the variables, so it's hard to recommend > alternatives. Keyword arguments can be useful for this sort of thing: > > function f(; a = 1, b = 2) > a + b > end > > f(a = 99, b = 2) > > You can also use Dict's to pass keyword arguments into functions as > follows: > > using Compat > d = @compat Dict(:a => 3, :b => 4) > > f(;d...) >
I think the dictionary expansion is exactly what I need, many thanks! > > Creating a type that holds the parameters is another good option, > depending on how the parameters will be used. > Yes that's also a good idea. I'm thinking about the best way to read parameters from a file and pass them into the simulation. If the parameters are stored e.g. in JSON format, then the JSON.jl module constructs a dictionary. So the dictionary unpacking seems to be the way to go. Thanks, David. > > > On Wed, Feb 11, 2015 at 3:25 PM, David P. Sanders <[email protected] > <javascript:>> wrote: > >> Hi, >> >> If I have a dictionary >> >> params = {"N": 10, "M": 2.0} >> >> how can I use it to define two variables N and M with the corresponding >> values? >> >> This sounds like it should be easy and obvious, say using `eval`? >> E.g. extract the keys and values into strings and then use >> >> eval(parse("N=10")) >> >> Is this reasonable? >> >> The use case is to load input parameters for a simulation. Maybe there is >> a better way? >> >> Thanks, >> David. >> > >
