Skip to content

Table Movement and Manipulation

Esteban Gomezllata Marmolejo edited this page Nov 9, 2024 · 2 revisions

Table Movement and Manipulation

Each table has a name (except the initial table until it gets named). We can use this name to move between different tables with $, but it can also be used to

The first time $ is used, we name the initial table, and anytime it is used with a new table name it creates a new table. $ on its own will move back to the initial table, and $ with an existing table name moves to that table.

# Calculates the square of user input
$main          # Name the initial table
$input ,       # Create new table and move into it, get user input
$              # Return to main
=input*input   # Calculate the square using our other table
.              # Output: Square of user input 

Tables with a single cell can be used like global variables.

# Calculate triangular numbers
$main    # Name the initial table
$x=0$    # "Create a variable x initialized to 0"
[10      # Loop 10 times
+x .     # Add x, ouput
$x+$     # Increase x by 1
]        # Loop

New tables have a single row and a single column. The number of rows or columns can be changed with \nrow and \ncol, which must take a positive value tail. New cells are 0 by default.

# Make a 2x5 table filled with zeros,
# except a 1 in the bottom left corner,
# then print it in Try EarScript using .2
=1 \nrow2 \ncol5 .2

The (0, 0) cell of a table is in the bottom left corner. This is because going up increases the vertical index is the rule we follow for consistency. In many cases, such as when thinking of "line numbers", it might make sense to print or interpret the top left corner as the origin, in which case ^ and ` swap roles.

We can move the pen using

  • > for right, increasing the horizontal index.
  • < for left, decreasing the horizontal index.
  • ^ for up, increasing the vertical index.
  • ` for down, decreasing the vertical index.

Note that the pen wraps around the table!

\ncol3     
=1>=2>=3>  # Initialize table to [1, 2, 3]
[12 .> ]   # Output: 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3

We can also use : to move to a specific column, and ; to move to a specific row, note that the indices of rows and columns start at 0, so :0 moves to the first column. This also has wrap-around, so for example :_1 moves to the last column.

Let's make an nxn table where the n is chosen by the user, and with 1 in the diagonal;

$main $n,$        # Get n from user input
\nrow_n \ncol_n   # Set the table size
;_1 :0            # Moves to the (n-1, 0) cell
[_n =>` ]         # Loop n times, set cell to 1, then move right and down.
.2                # Print the table

Finally, just like we can access the values of other tables' current cells, we can also access the values of other cells within the table thanks to relative tails such as +r, +l, +u or +d for the cell to the right, left, up or down. You can also use +2r for the cell two to the right, and so on.

# Nth Fibonacci number
$main $n,$        # Get n from user input
$n(le2 =1.~)-$    # Returns 1 is n is less than or equal to 2
\ncol2 =1 > =1 >  # Initialize table
[_n +l > ]        # N times, adds cell to the left, moves right
.                 # Output
# Pascal's Triangle
$main $n,(le1~)$  # Get n from user input
\nrow_n \ncol_n   # Set table size
;_1:0             # Get to top left cell
[_n=>]            # Set top row to 1s
$n-$              # Decrease n by 1
[_n               # Loop n times
  `=>             # Move down, set to 1, move right
  [_n +l +u > ]   # Add cell to the left and up, then move right
] .2              # End loop, print table

Next: Control Flow
Prev: Operators & I/O
Back to Wiki Home

Clone this wiki locally