next | previous | forward | backward | up | top | index | toc | Macaulay2 website
Macaulay2Doc > The Macaulay2 language > reading files

reading files

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.

First we create the file by writing the desired text to it.

i1 : fn = temporaryFileName()

o1 = /tmp/M2-3566-0/0
i2 : fn << "z^6+3*x*z^4+6*y*z^4+3*x^2*z^2+12*x*y*z^2+12*y^2*z^2+x^3+6*x^2*y+12*x*y^2+8*y^3" << endl << close

o2 = /tmp/M2-3566-0/0

o2 : File
Now we get the contents of the file, as a single string.
i3 : get fn

o3 = z^6+3*x*z^4+6*y*z^4+3*x^2*z^2+12*x*y*z^2+12*y^2*z^2+x^3+6*x^2*y+12*x*y^2
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.
i4 : R = ZZ/101[x,y,z]

o4 = R

o4 : PolynomialRing
i5 : f = value get fn

      6       4       4     2 2          2      2 2    3     2         2  
o5 = z  + 3x*z  + 6y*z  + 3x z  + 12x*y*z  + 12y z  + x  + 6x y + 12x*y  +

o5 : R
i6 : factor f

       2          3
o6 = (z  + x + 2y)

o6 : Expression of class Product
Often a file will contain code written in the Macaulay2 language. Let's create such a file.
i7 : fn << "sample = 2^100
     print sample
     " << close

o7 = /tmp/M2-3566-0/0

o7 : File
Now verify that it contains the desired text with get.
i8 : get fn

o8 = sample = 2^100
     print sample
To load and execute that code, use load. This is the function you will use most often for using bits of code you have previously written, unless you have incorporated them into a package, in which case you would use loadPackage.
i9 : load fn
The command needs can be used to load a file only if it hasn't already been loaded.
i10 : needs fn

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. One may use input for that.

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.

i11 : peek get fn

o11 = "sample = 2^100
      print 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.
i12 : lines get fn

o12 = {sample = 2^100, print sample}

o12 : List
We may use peek to observe the extent of these strings.
i13 : peek lines get fn

o13 = {"sample = 2^100", "print sample"}
We could even use stack to assemble the lines of the file into a net.
i14 : stack lines get fn

o14 = sample = 2^100
      print sample
Now let's remove that file.
i15 : removeFile fn