> < ^ Date: Tue, 28 Nov 1995 16:59:55 +0100
> < ^ From: Alexander Hulpke <hulpke@math.colostate.edu >
> ^ Subject: Re: Input to GAP

Dear GAP-Forum,

Stefan Neis asked:

I'm having some trouble finding out, how to read input from a file
into GAP. Say I produced a matrix and wrote it to a file using
"PrintTo". Is there any way to get it back into memory the next day?
"Read" doesn't seem to work, if I don't read files, which look like
the following line:
A:=Mat(Z,[[1,2],[3,4]]);
(i.e. I have to supply a command, not just an object, in the file.)

When printing the object, print it with
PrintTo("file","A:=",A,";\n");

There is no way within GAP to deduce a variables name from the variable, so
you'll have to supply the name yourself. Alternatively you can edit the file
to add the assignement and the semicolon (I'm sure you have found this
out already). A more comfortable way would be to use the following function:

SaveTo:=function(file,object,name)
  PrintTo(file,name,":=",object,";\n",
          "Print(\"#I  ",name," loaded\\n\");\n");
end;

The added print command prints the name of the defined variable as a
reminder when loading the file. Thus when saving an object using

SaveTo("file",A,"A");

`Read'ing "file" produces the reminder line

#I  A loaded

But with this method, I always have to remember, which variable I was
using, when creating the file...

As a slight variation, you could use the following pair of functions
(assuming you never use the global variable 'READ_OBJECT'):

PrintToObject := function ( file, object )
    PrintTo( file, "READ_OBJECT := \n", object, ";\n" );
end;

ReadObject := function ( file )
    Read( file );
    return READ_OBJECT;
end;

then you can save objects with PrintToObject("filename",A); and load them to
a variable by the assignement A:=ReadObject("file"); without any reference
to variable names used when saving the object.

Hope this helps,

Alexander Hulpke


> < [top]