Re: Storing and Loading Definitions / Emulating Associative Arrays

*To*: mathgroup at smc.vnet.net*Subject*: [mg93513] Re: Storing and Loading Definitions / Emulating Associative Arrays*From*: Albert Retey <awnl at gmx-topmail.de>*Date*: Thu, 13 Nov 2008 04:03:15 -0500 (EST)*References*: <gf3mfg$ekk$1@smc.vnet.net>

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: (* beginning of code *) data["data"] = RandomReal[{0, 1}, {100}] data["name"] = "random numbers" data["length"] = 100 data["max"] = Max[data["data"]] Put[ Function @@ {{data}, DownValues[data]}, ToFileName[{$HomeDirectory, "Desktop"}, "tst.m"] ] FilePrint[ToFileName[{$HomeDirectory, "Desktop"}, "tst.m"]] ClearAll[newdata] DownValues[newdata] = Get[ToFileName[{$HomeDirectory, "Desktop"}, "tst.m"]][newdata] newdata["length"] (* end of code *) this assumes that everything associated with data sits in its DownValues. You should also be aware that data will of course be replaced everywhere in the loaded expression with newdata (that is also in the RHS), so you need to take care that this causes no problems. You will also recognize that I used strings instead of symbols for the "keys" of data, which I would strongly recommend because it will save you from running into problems with context mismatches in case you would ever decide to put this code into a package. Another argument is that at least for older versions the lookup of strings was faster than for symbols, if I'm not mistaken. Finally, if your data is large, you could use: Export[ ToFileName[{$HomeDirectory, "Desktop"}, "tst.mx"], Function @@ {{data}, DownValues[data]} ] DownValues[newdata] = Import[ToFileName[{$HomeDirectory, "Desktop"}, "tst.mx"]][newdata] instead of Put and Get, which will save and load faster and create smaller files, but the resulting binary files can only be used on the same platform as they are created, that is you can't read them on linux if you created them on windows. hth, albert