Frosty
Frosty

Reputation: 33

Attempt to index local 'args' (a function value)

I was trying to split a string into a table, by using gmatch with %S+. However I ran into the error:

Attempt to index local 'args' (a function value)

Here are the three lines of code I believe have the problem:

print(msg)
local args = string.gmatch(msg, "%S+")
print(args[1])

So the first line print(msg) just prints a normal string, as it should. The second line is suppose to split that string by spaces, and store the table in args. The third line is suppose to print the first value in the table, however instead it gives me the error shown above. Thanks.

Upvotes: 3

Views: 1710

Answers (1)

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

string.gmatch doesn't return the results the way you expect it; it returns an iterator (a special function), which you can then use in a loop to get the values you need. That's why you get that error when you try to index the returned function.

You can check the documentation or this SO question for examples on how to use gmatch to get the values.

Upvotes: 6

Related Questions