Storing and Loading Definitions / Emulating Associative Arrays
- To: mathgroup at smc.vnet.net
- Subject: [mg93441] Storing and Loading Definitions / Emulating Associative Arrays
- From: Nikolaus Rath <Nikolaus at rath.org>
- Date: Sat, 8 Nov 2008 03:59:12 -0500 (EST)
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]
But what is the function something[] that I have to use for this
purpose? If I simply store the expression "data", then it is written
literally, without the associated definitions. If I use
Definition[data] then the output includes the name "data", so that I
cannot easily associate it with "data2" when loading. Is there a
simple way to achieve what I want?
I guess a way would be to store definitions as an anonymous function:
data = Function[{x}, Switch[x,
source, "simulation",
seed, 42,
parameters, {12, 0},
values, f[42, 12, 0]]];
Write[stream, data]
but then I still need a way to convert between the anonymous function
and the definitions when saving and loading. I do not want to work
with the anonymous function all the time, since it seems very
difficult to modify any parameters after the initial definition:
data = Read[Stream]
(* do some stuff *)
data[comment] = "Analysis on 11/7 showed that this situation is unli=
kely";
(* Write back the data with the comment *)
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