Correlated keys in Association object

110 Views Asked by At

I do need to setup an association in Mathematica, in which the value of a key is a function of the value of another key (of the same association-object). Currently I am putting a dummy value at when the Association is created and then, with a further operation, change the dummy variable to the correct value.

I would really do that directly during the declaration. Any trick for that?

SS = Association[n -> 1.0, x -> 2, dummy -> 0]
SS["dummy"] = 100*SS[[Key[n]]] + SS[[Key[x]]]
1

There are 1 best solutions below

4
On BEST ANSWER
f[n_, x_] := Association["n" -> n, "x" -> x, "dummy" -> 100*n + x]
SS = f[1., 2]

Edit: If you insist on a "one-liner":

SS=Function[{n,x}, Association["n" -> n, "x" -> x, "dummy" -> 100*n + x]][1.,2]

or even

SS=Association["n" -> #1, "x" -> #2, "dummy" -> 100*#1 + #2] &[1., 2]