Magma : Load file using variable file name

785 Views Asked by At

I want to load an external Magma file within another Magma file. (Both files are saved in the same directory.) I want to be able to quickly change which external file is being loaded, ideally at the beginning of the file making the load call, so that I can easily run the same code with various inputs.

(The external file contains computations, whose ultimate result is used by the file making the load call. These computations vary depending on the object being analyzed.)

I tried creating a string-type variable that stores the external file's name, then using Magma's load command with this variable. For example,

fileName := "externalMagmaFile.txt";
load fileName;

However, this results in the error

User error: Could not open file "fileName" (No such file or directory)

The same error results when I include double quotes around the external file name:

fileName := "\"externalMagmaFile.txt\"";
load fileName;

It seems that, for the load command, Magma interprets the variable name as the string specifying the file name, instead of first evaluating the variable, then executing load.

(I am using Magma V2.23-1 on MacOS Version 10.15.5.)

Can I use a variable with the load command in Magma? If yes, how?

2

There are 2 best solutions below

0
On

Here is sample code illustrating the approach proposed by @DavidCraven. (The file realFile.txt contains the Magma commands we desire to load.)

fileNameReal := "realFile.txt";
PrintFile("dummyLoadFile.txt","load \"" cat fileNameReal cat "\";" : Overwrite := true);
load "dummyLoadFile.txt";
0
On

Another solution (which is also a bit of a work around) is to set the Magma path based on which file you want to load using the SetPath() function. The Magma path indicates which directories are searched for loading Magma files.

To clarify, if you have 2 different files by the same name in separate directories (or can set it up so that you do) and want to choose which one to load, you can set filepath := "directory1" or filepath := "directory2" and then SetPath(filepath) before loading the file.

If you are concerned about changing the path, you can use GetPath() to save off the previous one and restore afterwards. Or add the new path to the end of the current path before calling SetPath().

Side note: I would have added this as a comment rather than an answer since it is a workaround and it is not the ideal solution for every scenario. But adding comments requires reputation, and I came upon this question because I was in a similar situation and this answer would have helped me. In my case I wanted to load several files from the same path (where the path is a variable determined at runtime) so this worked better for me than trying to do a dummyloadfile for each. Also, sadly, the true answer to your question (afaik) is that there is no direct way to load a file using a variable name.