0

i can use string.gsub(message, " ") but it only cuts the words.

i searched on http://lua-users.org/wiki/StringLibraryTutorial but i cant find any solution for this there

how can i save these words into variables? for example i have message = "fun 1 true enjoy"

and i want variables to have

var level = 1
var good = true
var message = "enjoy"
Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
  • 1
    Which Lua Version? With Lua 5.3.5 it is quite simple to iterate with: ```for word in message:gmatch('%w+') do print('Do here what you want with:',word) end``` – koyaanisqatsi Nov 24 '20 at 11:12

1 Answers1

5

Use string.match to extract the fields and then convert them to suitable types:

message =  "fun 1 true enjoy"
level,good,message = message:match("%S+%s+(%S+)%s+(%S+)%s+(%S+)")
level = tonumber(level)
 good = good=="true"
print(level,good,message)
print(type(level),type(good),type(message))

The pattern in match skips the first field and captures the following three fields; fields are separated by whitespace.

lhf
  • 70,581
  • 9
  • 108
  • 149