|
| 07 Oct 2013 03:00 PM |
How do u seperate values in a string that has commas? Like putting them into a table? String = "1,2,3,4,5,6,7,8,9,10" Into StringTable = {"1","2","3","4","5","6","7","8","9","10"}
♫♪ repeat wait() until game.Players.FrozenSmite.HasALife == true ♪♫ |
|
|
| Report Abuse |
|
|
breuning
|
  |
| Joined: 30 Oct 2008 |
| Total Posts: 4268 |
|
|
| 07 Oct 2013 03:02 PM |
s = "1,2,3,4,5,6,7,8,9,10" StringTable = {} for w in string.gmatch(s, ",") do table.insert(StringTable,w) end |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:04 PM |
That pulls out all the commas, but i understand now, ty.
♫♪ repeat wait() until game.Players.FrozenSmite.HasALife == true ♪♫ |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:07 PM |
function maketab(strin) string = strin.."," --have to add one local tab = {} local ns = "" for i = 1, string:len() do if string:sub(i,i) == "," then table.insert(tab, ns) ns = "" else ns = ns..string:sub(i,i) end end return tab end
|
|
|
| Report Abuse |
|
|
breuning
|
  |
| Joined: 30 Oct 2008 |
| Total Posts: 4268 |
|
|
| 07 Oct 2013 03:07 PM |
woooops
s = "1,2,3,4,5,6,7,8,9,10" StringTable = {} for w in string.gmatch(s, "%w+") do table.insert(StringTable,w) end |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:14 PM |
>>breuning Wouldn't your first code be better for this situation? (In case of decimals and etc) |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 07 Oct 2013 03:33 PM |
Here's a better version of breuning's code.
local string = "1,2,3,5,23,42,34,3" --random numbers seperated with commas local tab = { }
for num in string:gmatch("[^,]+") do table.insert(tab, num) end
for _, v in pairs(tab) do print(v) end |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:43 PM |
| dig, use print(unpack(tab))) or print(table.concat(tab, " ")) |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 07 Oct 2013 03:45 PM |
ProjectsHidden: I know I could do that, but that wont work for every kind of table.
table.concat() and unpack() only work then the table uses numerical indexes (aka {'item1', 'item2' ...}) |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:46 PM |
| In this case it is. Way to make you look smart. |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 07 Oct 2013 03:48 PM |
| ProjectsHidden: True, but in most cases you're not using numerical indexes in tables and you can get a little used to not doing that. |
|
|
| Report Abuse |
|
|
|
| 07 Oct 2013 03:51 PM |
| unpack works for anything. |
|
|
| Report Abuse |
|
|
digpoe
|
  |
| Joined: 02 Nov 2008 |
| Total Posts: 9092 |
|
|
| 07 Oct 2013 03:53 PM |
unpack does NOT work for anything.
I have proof it doesn't too.
Copied from the LuaForWindows Lua interpreter:
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio > print(unpack({Test="hi", "bye"})) bye
See how it doesn't print 'hi'?
If I run the command print(({Test="hi", "bye"}).Test) it works fine though.
Obviously it doesn't work. |
|
|
| Report Abuse |
|
|