-1

Is there something to do something with each word like put them in a table but without it being a list. So like a full sentence (string) like this:

skylift valkyrie2 airbus hunter adder bus armytanker armytrailer armytrailer2 baletrailer boattrailer
do ... for each word
    (put in table)
end
Nifim
  • 4,758
  • 2
  • 12
  • 31
  • use search on this site, [for example here](https://stackoverflow.com/questions/1426954/split-string-in-lua) – Mike V. Dec 29 '22 at 07:10
  • 1
    "put them in a table but without it being a list" - so what do you want? a set? – Luatic Dec 29 '22 at 09:22

1 Answers1

0

With gsub()?

€ /bin/lua
Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
> str = "skylift valkyrie2 airbus hunter adder bus armytanker armytrailer armytrailer2 baletrailer boattrailer"
> do local text, count = str:gsub('%w+', function(match) return(match):upper() end) print(text, count) end
SKYLIFT VALKYRIE2 AIRBUS HUNTER ADDER BUS ARMYTANKER ARMYTRAILER ARMYTRAILER2 BALETRAILER BOATTRAILER   11

You are free to do what you want in the function in gsub().
For example table.insert(gstab, match)

> gstab = {}
> gsfunc = function(match) local match = match:sub(1, 1):upper() .. match:sub(2, -1) table.insert(gstab, match) return(match) end
> str:gsub('%w+', gsfunc)
> print(table.concat(gstab, '\n'))
Skylift
Valkyrie2
Airbus
Hunter
Adder
Bus
Armytanker
Armytrailer
Armytrailer2
Baletrailer
Boattrailer
koyaanisqatsi
  • 2,585
  • 2
  • 8
  • 15