Learn Lua · Tables · Lesson 4 of 5

Splitting text

CoCoCo adds string functions you'd otherwise write yourself. string.split breaks text apart at a separator, trim removes spaces at both ends, and string.join puts a list back together:

LUA
string.split("a,b,c", ",")            --> {"a", "b", "c"}
("  hi  "):trim()                     --> "hi"
string.join(" | ", { "a", "b" })      --> "a | b"
string.contains("rush order", "rush") --> true

split gives a list, so you can loop over the parts with ipairs and build a new list with table.insert.

What your code receives

Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.

LUA
input = { tags = "gloss, urgent" }

Steps

  1. Return tags: the tags from input.tags, split at commas, with spaces trimmed from each.
  2. Add joined: those tags joined with " | ".
  3. Add urgent: true if input.tags contains the word urgent, otherwise false.
Show a hint

Split first, then build a new list of trimmed parts. Check for the word in the original text, before it's split.