• To: mathgroup at smc.vnet.net
• Subject: [mg93539] Re: Storing and Loading Definitions / Emulating Associative Arrays
• From: Nikolaus Rath <Nikolaus at rath.org>
• Date: Thu, 13 Nov 2008 21:08:07 -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
>>
>>
>
> If I correctly understand whay you are trying to doe, I think you
> could do something along the following lines:

Yes, that's exactly what I wanted, thanks a lot!

In case someone else is interested, here are the two functions that I

(* Save and load function definitions *)
SaveDefinition[fn_, file_] :=
(Put[{Function @@ {{fn}, DownValues[Evaluate[fn]]},
Function @@ {{fn}, UpValues[Evaluate[fn]]}},
file];)
data = Get[file];
Clear[fn];
DownValues[fn] = data[[1]][fn];
UpValues[fn] = data[[2]][fn];
];

They also save the UpValues and allow a nice syntax:

SaveDefinition[data, "file.dat"]

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

```

• Prev by Date: Re: Storing and Loading Definitions / Emulating Associative Arrays
• Next by Date: Re: Fourier Transform