Macaulay2 » Documentation
Packages » Macaulay2Doc > The Macaulay2 language > strings and nets
next | previous | forward | backward | up | index | toc

strings and nets -- an overview of strings and nets in Macaulay2

In this section we discuss strings and nets. Strings are sequences of characters intended to be printed, and are encountered in almost every programming language. Nets are two-dimensional arrays of characters intended to be printed, and constitute a natural generalization of strings that makes ascii printing of complicated objects much easier.


A string is a sequence of characters. Strings can be manipulated in various ways to produce printed output. One enters a string by surrounding a sequence of characters with quotation marks.
i1 : "abcdefghij"

o1 = abcdefghij
Strings may contain newline characters.
i2 : "abcde

o2 = abcde
Strings, like anything else, can be assigned to variables.
i3 : w = "abcdefghij"

o3 = abcdefghij

There are escape sequences that make it possible to enter special characters, see ".

i4 : u = "abc\101\102\n\tstu"

o4 = abcAB

We can use peek to see what characters are in the string.

i5 : peek u

o5 = "abcAB
Another way to enter special characters into strings is to use /// as the string delimiter. That makes it easier to enter the special characters into the string, but impossible to enter three consecutive slashes.
i6 : ///a \ n = "c"///

o6 = a \ n = "c"
The function ascii converts strings to lists of ascii character code, and back again.
i7 : ascii u

o7 = {97, 98, 99, 65, 66, 10, 9, 115, 116, 117}

o7 : List
i8 : ascii oo

o8 = abcAB
We may use the operator | to concatenate strings.
i9 : w|w|w

o9 = abcdefghijabcdefghijabcdefghij
The operator # computes the length of a string.
i10 : #w

o10 = 10
We may also use the operator # to extract single characters from a string. Warning: we number the characters starting with 0.
i11 : w#5

o11 = f
The function substring will extract portions of a string for us.
i12 : substring(5,w)

o12 = fghij
i13 : substring(5,2,w)

o13 = fg


A net is a rectangular two-dimensional array of characters, together with an imaginary horizontal baseline that allows nets to be assembled easily into lines of text. A string is regarded as a net with one row.

Nets are used extensively for such things as formatting polynomials for display on ascii terminals. Use net to obtain such nets directly.

i14 : R = ZZ[x,y];
i15 : (x+y)^2

       2           2
o15 = x  + 2x*y + y

o15 : R
i16 : n = net oo

       2           2
o16 = x  + 2x*y + y
The net n above looks exactly the same as the original polynomial - that's because a polynomial is printed by printing its net. But the net n is no longer usable as a polynomial; it's just an array of characters. We may use peek to clarify the extent of a net.
i17 : peek n

      | 2           2|
o17 = |x  + 2x*y + y |
One way to create nets directly is with the operator ||, which concatenates strings or nets vertically.
i18 : x = "a" || "bb" || "ccc"

o18 = a
We may use the operator ^ to raise or lower a net with respect to its baseline. Look carefully to see how the baseline has moved - it is aligned with the equal sign.
i19 : x^2

o19 = ccc
Nets may appear in lists, and elsewhere.
i20 : {x,x^1,x^2}

            a    bb
o20 = {a  , bb , ccc}
       bb   ccc

o20 : List
Nets and strings can be concatenated horizontally with the operator |.
i21 : x^2 | "-------" | x

o21 = ccc-------a
Each net has a width, a depth, and a height. The width is the number of characters in each row. The depth is the number of rows below the baseline, and the height is the number of rows above the baseline. The depth and the height can be negative.
i22 : width x, height x, depth x

o22 = (3, 1, 2)

o22 : Sequence
We may extract the rows of a net as strings with unstack and put them back together again with stack.
i23 : v = unstack x

o23 = {a, bb, ccc}

o23 : List
i24 : peek oo

o24 = {"a", "bb", "ccc"}
i25 : stack v

o25 = a


functions for handling strings

functions for handling nets

more information