Re: Storing and Loading Definitions / Emulating Associative Arrays

*To*: mathgroup at smc.vnet.net*Subject*: [mg93527] Re: Storing and Loading Definitions / Emulating Associative Arrays*From*: Nikolaus Rath <Nikolaus at rath.org>*Date*: Thu, 13 Nov 2008 21:05:57 -0500 (EST)*References*: <gf3mfg$ekk$1@smc.vnet.net> <gfgqgc$dk6$1@smc.vnet.net>

Albert Retey <awnl at gmx-topmail.de> writes: > Hi, > >> I have a routine that calculates some sequence of numbers. The >> calculation takes very long, so I would like to store the results in a >> file. >> >> What I would like to do is to pack the data, together with the >> parameters of its generation, into some sort of associative array. >> Since I couldn't find such a thing in Mathematica, I came up with the >> idea of using definitions instead: >> >> >> f[seed_, p_, n_] = (* Complicated computation *) {2,4,4}; >> data[source] = "simulation"; >> data[seed] = 42; >> data[parameters] = {12, 0}; >> data[values] = f[data[seed], Sequence @@ data[parameters]] >> >> Now I would like to (somehow) store this set of definitions: >> >> Write[stream, something[data]] >> >> and later load it again, possibly with a different name >> >> data2 = Read[stream] >> > > If I correctly understand whay you are trying to doe, I think you could > do something along the following lines: > > Put[ > Function @@ {{data}, DownValues[data]}, > ToFileName[{$HomeDirectory, "Desktop"}, "tst.m"] > ] > > DownValues[newdata] = > Get[ToFileName[{$HomeDirectory, "Desktop"}, "tst.m"]][newdata] > Yes, that does exactly what I want. Thanks a lot! I don't think I'd have come up with that tricky code anytime soon. Best, -Nikolaus -- =C2=BBIt is not worth an intelligent man's time to be in the majority. By definition, there are already enough people to do that.=C2=AB -J.H. Hardy PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6 02CF A9AD B7F8 AE4E 425C