Home > Net >  lua split string and save in lua table
lua split string and save in lua table

Time:10-26

Is there an way to split an string and save it into an table like this:

str = "23 = John, 45 = Karl, 6 = Chloe, 34 = Sarah" 

--[[ 23     John
     45     Karl
     6      Chloe
     34     Sarah]]

I want the numbers to be the keys and the Names to be the values.

CodePudding user response:

Adapt this code:

for k,v in str:gmatch("(%d )%s*=%s*(%a )") do
    print(k,v)
end

This assumes that the names are composed of letters only.

  • Related