Printing a List
What happens when you print a whole list?
local crops = {"carrot", "potato", "pumpkin"}
print(crops)This prints something like:
table: 0x109008Instead of the items, Lua prints the type, table, and a number written in hexadecimal (with the digits 0 to 9 and the letters a to f). It's the table's address, the place in the computer's memory where the table is. It's useful only to tell two tables apart, and it can be different each time you run the program. Joining a list to a text doesn't work either: "Crops: " .. crops ends with attempt to concatenate a table value (local 'crops').
To print the items, join them into one text with table.concat. Its first argument is the list, and the second one is the text that goes between the items, the separator:
local crops = {"carrot", "potato", "pumpkin"}
print(table.concat(crops, ", "))
print("Crops: " .. table.concat(crops, ", "))
print(table.concat(crops, " | "))
print(table.concat(crops))This prints:
carrot, potato, pumpkin
Crops: carrot, potato, pumpkin
carrot | potato | pumpkin
carrotpotatopumpkinThe separator goes only between the items, not after the last one. Without a separator, the items are glued together. The result is an ordinary text, so you can join it to other texts or store it in a variable. For an empty list, table.concat gives an empty text.
The items can be texts and numbers, but nothing else. For any other value, such as true, the error message says which item is wrong:
print(table.concat({"carrot", true, "pumpkin"}, ", "))main.lua:1: invalid value (boolean) at index 2 in table for 'concat'Sometimes you need more than the items themselves, for example the number of days next to each crop. Then build a new list of texts in a loop, and join it at the end:
local crops = {"carrot", "potato", "pumpkin"}
local days = {3, 4, 7}
local labels = {}
for i, crop in ipairs(crops) do
labels[#labels + 1] = crop .. " (" .. days[i] .. " days)"
end
print(table.concat(labels, ", "))This prints:
carrot (3 days), potato (4 days), pumpkin (7 days)You can also print the items with io.write in a loop, as in the lesson on loops. Then you have to take care of the separator yourself: it goes before every item except the first one.
local crops = {"carrot", "potato", "pumpkin"}
for i, crop in ipairs(crops) do
if i > 1 then
io.write(", ")
end
io.write(crop)
end
print()This prints:
carrot, potato, pumpkinThe print() at the end finishes the line. table.concat does all of this in a single line.
Instructions
Run the program: it prints the address of the table, not the goods. Change the last line so that it prints the goods with table.concat: For sale: carrots, potatoes, pumpkins, eggs.
Below it, create an empty local list labels. Then go through goods in a loop, and for each item, add a text such as carrots (12) to the end of labels: the goods, a space, and the amount from amounts in parentheses.
After the loop, print labels with table.concat: Today: carrots (12), potatoes (8), pumpkins (3), eggs (30).
Start programming for free
7/11