Lists and records
Sometimes one value is really several. A person’s hobbies are a list: values in order, all of the same kind. An address is a record: named fields kept together. Quill uses those two words, so the code says what it is.
A list
Write list, then the items, separated by commas:
hobbies = list "coding", "reading", "traveling"
say hobbies
say hobbies at 1
coding, reading, traveling
coding
- Every item is the same kind. A list of text stays a list of text.
atpicks one item. Lists count from 1, sohobbies at 1is"coding"andhobbies at 3is"traveling".- An empty list says its kind, because there is no item to learn it from:
list of text,list of number, orlist of true/false.
Where of and at are used says when each word is required.
A list of numbers works the same way: prices = list 12.50, 4, 8.25.
Mixing kinds is a mistake:
hobbies = list "coding", 2
Problem on line 1 of example.quill:
hobbies = list "coding", 2
^
Every item in a list has to be the same kind.
This list holds Text, but 2 is Number.
Square brackets are a mistake too. Quill’s name for this is list:
hobbies = ["coding", "reading"]
Problem on line 1 of example.quill:
hobbies = ["coding", "reading"]
^
Quill writes a list with the word list, not with square brackets.
A list is values of one kind, in order: list "coding", "reading".
Did you mean: list "coding", "reading"
Where of and at are used
of and at are reserved words, so they can’t be names. Each one has a single job, and in that job it is required.
at picks one item
Write the list, then at, then the position. The position is a whole number, and 1 is the first item:
hobbies = list "coding", "reading", "traveling"
say hobbies at 2
reading
Use at whenever you want one item by its place. To change that one item, the list has to be a var, and at stays in the set:
var hobbies = list "coding", "reading"
set hobbies at 2 = "writing"
say hobbies at 2
writing
at is for one position. Walking every item is for hobby in hobbies, which uses in. A field of a record is a dot: address.city. Replacing the whole list is set hobbies = list "cooking", with no at.
of names an empty list
A list that already has items leaves of out. The items show the kind: list "coding", "reading" is a list of text.
An empty list has no items to learn from, so of is required. The kind after it is text, number, or true/false:
waiting = list of text
prices = list of number
flags = list of true/false
say waiting
say prices
say flags
an empty list
an empty list
an empty list
A list with nothing after it is a mistake. Quill needs the items, or of and a kind:
waiting = list
Problem on line 1 of example.quill:
waiting = list
^^^^
list needs its items after it, separated by commas.
An empty list names its kind: list of text, list of number, or list of true/false.
Did you mean: list "coding", "reading"
Walking a list
for can walk a list, once for each item. The name holds the item, and it exists only under the for:
hobbies = list "coding", "reading", "traveling"
for hobby in hobbies
say hobby
coding
reading
traveling
The list itself is not changed. To keep a total, create a var before the loop, the same way a counting loop does.
Text and lists: split, join, contains
split TEXT by SEPARATOR cuts text into a list of texts, at each separator. It is how a line from a file or a form becomes pieces you can use:
line = "Amina,80,paid"
parts = split line by ","
say parts at 1
say parts at 2
say count of parts
Amina
80
3
The separator is left out of the pieces. Two separators side by side give an empty piece between them, so split "a,,b" by "," has three pieces: "a", "", and "b".
join LIST with SEPARATOR goes the other way. It puts the items of a list into one text, with the separator between them:
hobbies = list "coding", "reading", "traveling"
prices = list 3, 12.50, 7
say join hobbies with " and "
say join prices with " | "
coding and reading and traveling
3 | 12.50 | 7
A list of numbers or money joins too. Each item is shown the way say shows it.
contains works on a list as well as on text. It is true when the item is in the list:
hobbies = list "coding", "reading"
say hobbies contains "coding"
say hobbies contains "chess"
true
false
The item has to be the same kind as the list’s items, so hobbies contains 5 is a mistake.
split works on money too, where it shares an amount out without losing a cent. That is in Money. For text, split always takes by and the text between the pieces.
A record
A record is a group of named fields. Put record on its own line, then each field underneath, indented by exactly 2 spaces. A field is a name, then =, then a value:
address = record
street = "123 Main St"
city = "Anytown"
state = "CA"
zip = "12345"
say address.city
Anytown
A dot reads one field: address.city is "Anytown". say address shows every field:
address = record
street = "123 Main St"
city = "Anytown"
say address
street: 123 Main St
city: Anytown
A short record can sit on one line: address = record city = "Anytown", state = "CA". Braces are not how Quill writes a record. The word is record.
Fields can be different kinds. city can be text while another field is a number. Two records are the same kind when they have the same field names and each field has the same kind. The order you write the fields does not matter for that.
When a value wraps another
One school is a record. The fields belong to that one school, so sum reads them directly:
school = record name = "Harvard", fees = 10000
say sum fees of school
10000
A list wraps several records when they are the same kind of thing and one line should read all of them:
harvard = record name = "Harvard", fees = 10000
oxford = record name = "Oxford", fees = 12000
schools = list harvard, oxford
say sum fees of schools
22000
schools exists so the two fees can be added together. One school has no list around it.
A record wraps a list when one thing owns many items:
up = record place = "Up Station"
old = record place = "Old Town"
desk = record bookings = list up, old
say get place of desk.bookings
Up Station, Old Town
desk is one desk. bookings is the list it owns. get place of desk.bookings reads a field from that list.
Changing one piece
The whole list or record has to be marked var. Then set can replace one item or one field:
var hobbies = list "coding", "reading"
set hobbies at 2 = "writing"
say hobbies
var address = record city = "Anytown"
set address.city = "Springfield"
say address.city
coding, writing
Springfield
set hobbies = list "cooking" replaces the whole list. The new list still has to be a list of the same kind.
A record that isn’t marked var stays as it was written. Change it by writing var where it is created, then set the field.
Asking for one field
When the list holds records, sum and get read one field from all of them. One record gives that one amount: sum fees of school. get name, fee of roster keeps several fields together. A dot reaches a field further in: get place of desk.bookings, or get gpa of school.acceptances. count of roster says how many records the list has, average fee of roster gives the typical fee, and roster sorted by fee puts them in order. Asking for data shows each form. for stays when each record needs its own step.
sisters = record name = "Nchang Sisters", fee = 80000 USD
brass = record name = "Bamenda Brass", fee = 150000 USD
roster = list sisters, brass
say sum fee of roster
say get fee of roster
230000 USD
80000 USD, 150000 USD
Try it
- Make a list of three prices and say the second one.
- Walk that list with
forand add the prices into avar total. - Make a record for a person, with a name and an age, and say the name.
split "red,green,blue" by ",", thenjointhe pieces backwith " / ".