How to print long strings in MAGMA?

1.1k Views Asked by At

I'm trying to print a very long list to a file using magma to make it "callable". The list is called F2, and the code I'm using to print this list to a file is the following

SetOutputFile("...");
printf "F2 := %o ;", F2;

(where, of course, ... is my file directory) however, the list in the file is always incomplete, that is, the list is cut in some part.

I have not been able to determine the causes of this issue. I tried again with two lists, F1 and F2, using the following code:

SetOutputFile("...");
printf "F1 := %o ; \n F2 := %o ;", F1, F2;

and, weirdly, F1 is printed completely while F2 is not.

I really appreciate any information you can give me. If I need to add some specifications about the system I'm using just let me know.

1

There are 1 best solutions below

1
On BEST ANSWER

You are using the wrong mechanism for printing to a file in MAGMA. What you are doing is setting a file for the normal MAGMA buffer to output to; this means what would normally be printed to the screen is instead routed to a file. I'm guessing your intention is to write this MAGMA object to a file so you can read it in later in new MAGMA sessions. In this case, you want to use the PrintFile command:

PrintFile(<filename>, "F1 := ");
PrintFile(<filename>, F1);
PrintFile(<filename>, ";\n F2 := ");
PrintFile(<filename>, F2);
PrintFile(<filename>, ";");

It can be a bit tricky to get MAGMA to read back in these objects from the file in an efficient way; what I tend to do is to save a single object to a file, using PrintFileMagma, which makes sure this is written to the file in the proper MAGMA format:

PrintFileMagma("F1.mgo", F1);

Then I have a custom function defined in my startup file to read these back in:

FOB := function(N)
    s := Read(N);
    OB := eval s;
    return OB;
end function;

Then in a new MAGMA session I can simply type

F1 := FOB("F1.mgo");

to import the desired object.

Edit: It was mentioned that MAGMA doesn't write some objects like AssociativeArray properly in Magma mode.

One workaround for this particular case is to save the AssociativeArray as a list of tuples,

PrintMagmaMode("filename.mgo", [* <k,A[k]> : k in Keys(A) *]);

and then convert back into an associative array when you load using.. well it seems a for loop is the only way to initialize an associative array.

A := AssociativeArray();
for a in FOB("filename.mgo") do
   A[a[1]] := a[2];
end for;