Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: [Lua] Splitting strings again  (Read 4771 times)

0 Members and 1 Guest are viewing this topic.

Lemonilla

    Topic Starter


    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
[Lua] Splitting strings again
« on: November 15, 2013, 04:59:52 PM »
So I have a table that contains tables that holds a line from a text file, so if the file's first line is "0,0,0,50" then table = { 1 = {a = "0,0,0,50"} 2 = { a = "LINE 2"} 3 = { .....etc.  }  I want to seperate t[1][a] by commas and store each of them in their respective places within the table.

so what I want to do is have it like this:
table = {
        1 = {
                a = "0,0,0,50"
                1 = 0
                2 = 0
                3 = 0
                4 = 50
        }
}

What I have so far:
function split_comma_String()
   local b = tablelength(t)
   print(t[1].a)
   print(b)
   repeat
      local k = 1
      local tak = t.a
      print(tak)
      for v in string.gmatch(tak, "%w+") do -- error in "%w+"    Want to seperate by comma (,)
         t[k] = v
         k = k + 1
         print(b,b,t[k])
      end
      b = b - 1
   until b == 0
end

Nothing that I put in the quotes seems to work.  If someone could explain why this is not working that would be very helpful.
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

Lemonilla

    Topic Starter


    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: [Lua] Splitting strings again
« Reply #1 on: November 16, 2013, 09:48:43 AM »
Oops, I forgot to put it in quotes, so the syntax got all messed up.
Code: [Select]
function split_comma_String()
local b = tablelength(t)
print(t[1].a)
print(b)
repeat
local k = 1
local tak = t[b].a
print(tak)
for v in string.gmatch(tak, "([^,]+)") do -- error in "\,+"    Want to separate by comma (,)
t[b][k] = v
k = k + 1
print(b,b,t[b][k])
end
b = b - 1
until b == 0
end

Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.

Lemonilla

    Topic Starter


    Apprentice

  • "Too sweet"
  • Thanked: 70
  • Computer: Specs
  • Experience: Experienced
  • OS: Windows 7
Re: [Lua] Splitting strings again
« Reply #2 on: November 16, 2013, 10:12:02 AM »
Well I got it to split correctly by using "[^,]+" (though I have no idea why that works).  But now it won't set the value into the table.

current code:
Code: [Select]
function split_comma_String()
local b = 1
--print(t[1].a)
--print(b)
repeat
local k = 0
local tak = t[b].a
--print(tak)
for v in string.gmatch(tak, "[^,]+") do
_G["t["..b.."]" ..k] = v        -- Not working
k = k + 1
print(v ,"t", b, k, t[b][k])
end
print("")
b = b + 1
until b == t_length
end
Quote from: patio
God Bless the DOS Helpers...
Quote
If it compiles, send the files.