Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

lua, split string

Engaged ,
Dec 10, 2015 Dec 10, 2015

This is almost embarrassing to ask about but I'm stuck. I'm trying to split a string at whitespace. I'm broadly assuming that string.gmatch() returns an array, which it appears to do in the loop example below. However if I try and grab an element from the return value it throws up an error, telling me it's a function. Can anyone help:?

local example = "an example string"

-- works as expected.
 for i in string.gmatch(example, "%S+") do
  Debug.logn(i)
end

-- tells me s is a function
Debug.logn( string.gmatch( example, "%S+" ) )

-- tells me I'm trying to index a function value
Debug.logn( s[1] ) 
TOPICS
SDK
1.3K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 10, 2015 Dec 10, 2015

string.gmatch() returns an iterator (not an array) that can only be used in a for loop.   You may want to use one of the other matching functions:

Untitled.png

That comes from the Lua 5.1 Short Reference, which I always keep close at hand.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Dec 24, 2015 Dec 24, 2015
LATEST

Thanks John.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines