Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split a string and store in an array in lua

Tags:

string

split

lua

I need to split a string and store it in an array. here i used string.gmatch method, and its splitting the characters exactly, but my problem is how to store in an array ? here is my script. my sample string format : touchedSpriteName = Sprite,10,rose

objProp = {}
for key, value in string.gmatch(touchedSpriteName,"%w+") do 
objProp[key] = value
print ( objProp[2] )
end

if i print(objProp) its giving exact values.

like image 904
ssss05 Avatar asked Oct 03 '12 13:10

ssss05


People also ask

How do you split a string into an array?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

How do you slice a string in Lua?

A very simple example of a split function in Lua is to make use of the gmatch() function and then pass a pattern which we want to separate the string upon.

How do I split a string into multiple parts?

Answer: You just have to pass (“”) in the regEx section of the Java Split() method. This will split the entire String into individual characters.

What does string Sub Do in Lua?

Another important function of the Lua's string library is the string. sub() function. The string. sub() function is used to extract a piece of the string.

Is there a string splitting function in Lua?

Unfortunately, in LUA, there is no inbuilt string splitting function, which is very inconvenient. However, you could use string.gmatch to begin a regular expression matching and the following is a short and neat alternative. A string splitting function will split a source string according to a delimiter (char separator), into a array of substrings.

What is an array in Lua?

Definition of Lua Array An array is used to store the elements in Lua, array represents the ordered set of an object that we store inside it. Array can be one dimensional or multi-dimensional in Lua.

How to split a string into an array of strings?

A string splitting function will split a source string according to a delimiter (char separator), into a array of substrings. This usually requires string parsing. Unlike other programming language, the string concatenation is made possible by double dots “..”. loading...

What is the difference between one-dimensional and multi-dimensional array in Lua?

If we have created a one-dimensional array then it will contain several rows inside it, on the other hand on the creation of a multi-dimensional array will contain multiple columns and rows. Array in Lua uses indexing table, size of the array in Lua is not fixed can be changed or grow at runtime or based on the requirement.


2 Answers

Your expression returns only one value. Your words will end up in keys, and values will remain empty. You should rewrite the loop to iterate over one item, like this:

objProp = { }
touchedSpriteName = "touchedSpriteName = Sprite,10,rose"
index = 1

for value in string.gmatch(touchedSpriteName, "%w+") do 
    objProp[index] = value
    index = index + 1
end

print(objProp[2])

This prints Sprite (link to demo on ideone).

like image 111
Sergey Kalinichenko Avatar answered Oct 20 '22 01:10

Sergey Kalinichenko


Here's a nice function that explodes a string into an array. (Arguments are divider and string)

-- Source: http://lua-users.org/wiki/MakingLuaLikePhp
-- Credit: http://richard.warburton.it/
function explode(div,str)
    if (div=='') then return false end
    local pos,arr = 0,{}
    for st,sp in function() return string.find(str,div,pos,true) end do
        table.insert(arr,string.sub(str,pos,st-1))
        pos = sp + 1
    end
    table.insert(arr,string.sub(str,pos))
    return arr
end
like image 27
Darkwater Avatar answered Oct 19 '22 23:10

Darkwater