1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Do you now the pattern if it is like :string = " 10 = Hello_World, 4 = Hello_World_Good_Bye"
@Glupschi read Lua 5.4 Reference Manual: 6.4.1 Patterns try it yourself or you won't learn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.