Previous Up Next

10.4.2  Writing variable values to a file

You can save variable values to a file, to be read later, with the write command. This command takes a string for a file name and a list of variables to save. For example, if a has the value 3.14 and b has the value 7, then

write("foo",a,b)

will create a file named “foo” with the contents

  a:=(3.14);
  b:=7;

If you wanted to store the first million digits of π to a file, you could set it equal to a variable

pidec := evalf(pi,10^6):;

and then store it in a file

write("pi1million",pidec)

If you want to restore the values of variables saved this way, for example in a different session or if you have purged the variables, then you can use the read command, which simply takes a file name as a string. If, in a different session, you want to use the values of a and b above, the command

read("foo")

will give them the values 3.14 and 7 again. Note that this will silently overwrite any values that a and b might have had.


Previous Up Next