Sometimes a file will contain a single expression whose value you wish to have access to. For example, it might be a polynomial produced by another program. The function get can be used to obtain the entire contents of a file as a single string. We illustrate this here with a file whose name is expression.
i1 : get "expression" |
This isn't quite what you want, because a string containing a description of a polynomial is not the same as a polynomial. We may use value to evaluate the string and produce the corresponding polynomial, after first setting up a polynomial ring to contain it.
i2 : R = ZZ/101[x,y,z] |
i3 : f = value get "expression" |
i4 : factor f |
Often a file will contain code written in the Macaulay 2 language. We have a file called sample with two lines of code.
i5 : << get "sample"; |
To load and execute that code, use load.
i6 : load "sample" |
The command needs can be used to load a file only if it hasn't already been loaded.
i7 : needs "sample" |
For debugging or display purposes, it is sometimes useful to be able to simulate entering the lines of a file one by one, so they appear on the screen along with prompts and output lines. We use input for this.
i8 : input "sample" |
There are other ways to manipulate the contents of a file with multiple lines. First, let's use peek to observe the extent of this string returned by get.
i12 : peek get "sample" |
The resulting string has newlines in it; we can use lines to break it apart into a list of strings, with one row in each string.
i13 : lines get "sample" |
We may use peek to observe the extent of these strings.
i14 : peek lines get "sample" |
We could even use stack to assemble the lines of the file into a net.
i15 : stack lines get "sample" |