We can define names for values in Pyret, the same way we do in math:

name = "Flannery"
age = 16
logo = star(50, "solid", "red")

When looking up a data Row from a Table, programmers use the row-n method. This method takes a single number as its input, which tells the computer which Row we want. Note: Rows are numbered starting at zero!

For example:

sasha   = animals-table.row-n(0)  # define Sasha to be the first row
mittens = animals-table.row-n(2)  # define Mittens to be the third row

When we define these rows, it’s more useful to name them based on their properties, rather than their identifiers:

cat-row = animals-table.row-n(0)  # Sasha is a cat
dog-row = animals-table.row-n(10) # Toggle is a dog

When looking up a column from a Row, programmers use square brackets and the name of the column they want.

For example:

# these two lines do the same thing! We can use the defined name to simplify our code
animals-table.row-n(0)["age"]  # look up Sasha's age (in row 0)
cat-row["species"]             # look up Sasha's age (using the defined name)
dog-row["age"]				   # look up Toggle's age (using the defined name)

These materials were developed partly through support of the National Science Foundation, (awards 1042210, 1535276, 1648684, and 1738598). CCbadge Bootstrap by the Bootstrap Community is licensed under a Creative Commons 4.0 Unported License. This license does not grant permission to run training or professional development. Offering training or professional development with materials substantially derived from Bootstrap must be approved in writing by a Bootstrap Director. Permissions beyond the scope of this license, such as to run training, may be available by contacting contact@BootstrapWorld.org.