Macaulay2 » Documentation
Packages » Macaulay2Doc » The Macaulay2 language » system facilities » getting input from the user
next | previous | forward | backward | up | index | toc

# getting input from the user

The function read can be used to prompt the user and obtain a line of input as a string. In response to the prompt, the user enters sample and press return.

    i1 : filename = read "file name : "
    file name : sample

    o1 = sample

Let's use peek to verify that this string contains no newline characters.

    i2 : peek filename

    o2 = "sample"

If necessary, we may use value to evaluate the string provided by the user. In this example, we enter (x+y)^2 in response to the prompt.

    i3 : R = ZZ[x,y];

    i4 : s = read "polynomial : "
    polynomial : (x+y)^2

    o4 = (x+y)^2

    i5 : value s

          2           2
    o5 = x  + 2x*y + y

    o5 : R

The source of this document is in /build/macaulay2-88fgJW/macaulay2-1.25.11+ds/M2/Macaulay2/packages/Macaulay2Doc/ov_files.m2:198:0.