0
$\begingroup$

I have a notebook where I calculate integrals that take a long time to evaluate.

Let's say that I assign each evaluation to a symbol:

a1=some difficult integral
a2=some other difficult integral

In a later session I want to continue my calculations with those symbol a1, a2 and more. So one way is to write into a file the values:

Put[a1,a2,...,{filename}]

But when I use Get in a later session, Mathematica only takes the first line of the file:

Get["filename"]
(*prints only the value of a1*)

So a1=Get["filename"] gives the value for a1, but how do I get the second line to be the value of a2?

The option of using DumpSave works fine but it creates a binary file and I prefer non-binary files. Is there a way to do that with using plain text streams?

$\endgroup$
1
  • $\begingroup$ How about exporting as an .m file? $\endgroup$
    – Hans Olo
    Oct 30, 2022 at 18:16

1 Answer 1

1
$\begingroup$

From the help of Get:

"reads in a file, evaluating each expression in it and returning the last one."

Therefore, to use Put/Get you would need to write the definitions, not only the values. To write the definition, you need "Save". Here is a simple example:

a1=1;
a2=2;
a3=3;

We save the definitions to a file:

filename= "d:/tmp/test.dat";
DeleteFile[filename] // Quiet; (*if the file exists, output would be appended*)
Save[filename,{a1,a2,a3}]

We now delete a1..a3:

Clear[a1,a2,a3]

Finally we read the saved definitions:

Get[filename];

Get would return only the last value: , but it nevertheless defines a1..a3.

We can now check the values of a1..a3:

a1
a2
a3

(* 1 2 3 *)

Finally the saved file looks like:

![enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.