Splitting into an array

Basic types / Strings

-- gmatch gives the pieces that fit the pattern
local function split(text, pattern)
  local parts = {}
  for part in text:gmatch(pattern) do
    parts[#parts + 1] = part
  end
  return parts
end

local str_data = "1981|Kim Victoria|engineer"
-- [^|]+ is a run of any characters except |
local lst_data = split(str_data, "[^|]+")
-- lst_data = 1981, Kim Victoria, engineer

-- Unpacking: one name per part
local year_text, name, position =
  table.unpack(lst_data)
-- name = Kim Victoria
-- position = engineer
local year = tonumber(year_text)
-- year = 1981

-- %S+ is a run of non-space characters:
-- any run of spaces is one separator
local words = split("red  green   blue", "%S+")
-- words = red, green, blue

-- Empty fields are lost
local csv = "red,,green"
local lost = split(csv, "[^,]+")
-- lost = red, green

-- Keep them: add a separator to the end,
-- match a field with its separator
local kept = split(csv .. ",", "([^,]*),")
-- kept = red, , green

print("lst_data = "
      .. table.concat(lst_data, ", "))
print("name = " .. name)
print("position = " .. position)
print("year = " .. year)
print("words = " .. table.concat(words, ", "))
print("lost = " .. table.concat(lost, ", "))
print("kept = " .. table.concat(kept, ", "))