0

Is there a way to save a structure to a file in GAP? For example :

data:=rec();
data.Name:="random matrix";
data.Value:=RandomMat(3,4);
data.Rank:=2;

How would I save "data" to a file so it can be read from GAP at a future session? and how would I read it back in. (I don't want to save the entire workspace, just the records I want to use later).

unknown
  • 1,018

1 Answers1

1

There is no dedicated functionality for saving a single object. Using Print (or PrintTo a file) should produce GAP-readable input, but you need to supply the assignment syntax yourself, and you will lose information caches inside objects, or use of compressed data types.

In your case, you could use

PrintTo("mydatafile","data:=",data,";\n");

so that Read("mydatafile"); would give you an object in a new session.

ahulpke
  • 18,416
  • 1
  • 21
  • 38
  • Thanks, this will work but that seems to force "data" to be a global variable. So if I have a function with local variable "data" then Read("mydatafile") will not assign the record to the local variable. – unknown Apr 02 '22 at 15:45
  • 1
    If it must be a local variable, have a look at the documentation for ReadAsFunction, whioch allows you to assign return values to local variables. – ahulpke Apr 02 '22 at 18:15
  • ReadAsFunction does the job...thanks – unknown Apr 02 '22 at 18:52