Kondou
|
  |
| Joined: 05 Aug 2012 |
| Total Posts: 1148 |
|
|
| 01 Sep 2013 04:49 PM |
Is it possible to have the contents of a table go into a text label?
[WIJ Sergeant] Kondou |
|
|
| Report Abuse |
|
|
ZachBloxx
|
  |
| Joined: 26 Jun 2013 |
| Total Posts: 2833 |
|
|
| 01 Sep 2013 04:56 PM |
t = {1,2,3,4,5} textLabel.Text = "Table Contents: " ..table.concat(t, ', ') |
|
|
| Report Abuse |
|
|
Usering
|
  |
| Joined: 18 Aug 2012 |
| Total Posts: 10281 |
|
|
| 01 Sep 2013 05:27 PM |
| @Zach, you should make it so when it hits the last object in the table it says: ", and " instead of just "," |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2013 05:35 PM |
function concat(table) local x,z = "" for i,v in pairs(table) do x = x..(not z and v or i<#table and ", "..v or ", and "..v) z=true end return x end |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2013 05:38 PM |
| The above (iterations' post) will error on inputs that have non-integer indices. That means you should swap pairs for ipairs; since ipairs is less efficient it may be preferable to replace it with simple iteration from 1 to #table. |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2013 05:40 PM |
dont use anything but numbahs D:
function concat(table) local x,y,z = "" for i,v in pairs(table)do y=i end for i,v in pairs(table) do x = x..(not z and v or i==y and ", "..v or ", and "..v) z=true end return x end
that should work |
|
|
| Report Abuse |
|
|
Kondou
|
  |
| Joined: 05 Aug 2012 |
| Total Posts: 1148 |
|
|
| 01 Sep 2013 05:41 PM |
Thanks for the help guys, I got it to work.
One last question about tables. How can you clear everything in a table?
[WIJ Sergeant] Kondou |
|
|
| Report Abuse |
|
|
| |
|
|
| 01 Sep 2013 05:41 PM |
function concat(table) local x,y,z = "" for i,v in pairs(table)do y=i end for i,v in pairs(table) do x = x..(not z and v or i~=y and ", "..v or ", and "..v) z=true end return x end |
|
|
| Report Abuse |
|
|
ZachBloxx
|
  |
| Joined: 26 Jun 2013 |
| Total Posts: 2833 |
|
|
| 01 Sep 2013 05:42 PM |
t = {1,2,3,4,5} for i,v in pairs(t) do table.remove(t,i) end |
|
|
| Report Abuse |
|
|
| |
|
|
| 01 Sep 2013 05:43 PM |
^ That will only affect the one reference. If we have
a = {}; b = a;
Then
a = {}
will not empty b.
I'm not sure if this will actually delete the entries or just make them nil, but I believe it does:
for i,v in pairs(tab) do tab[i] = nil; end |
|
|
| Report Abuse |
|
|
Usering
|
  |
| Joined: 18 Aug 2012 |
| Total Posts: 10281 |
|
|
| 01 Sep 2013 05:44 PM |
tab = {"hello","hi","lol",1,2,3}
table = {concat = function(tab, splice, lastSplice) finishedString = "" for i = 1,#tab do if i == #tab then finishedString = finishedString .. splice .. lastSplice .. tab[i] elseif i == 1 then finishedString = finishedString .. tab[i] else finishedString = finishedString .. splice .. tab[i] end end return finishedString end }
print(table.concat(tab,", ","and ")) |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2013 05:45 PM |
| @Taslem, that still wouldn't empty b. |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2013 05:47 PM |
function concat(table,a,b) local x,y,z = "" for i,v in pairs(table)do y=i end for i,v in pairs(table) do x = x..(not z and v or i~=y and a..v or b..v) z=true end return x end
print(concat({4,6,7,A=4, B=true}, ", ", ", and "))
yer |
|
|
| Report Abuse |
|
|
| |
|
ZachBloxx
|
  |
| Joined: 26 Jun 2013 |
| Total Posts: 2833 |
|
|
| 01 Sep 2013 05:50 PM |
Yes, Taslem's would work. Try this:
tab = {1,2,3,4,5} print(unpack(tab)) for i,v in pairs(tab) do tab[i] = nil end print(unpack(tab)) |
|
|
| Report Abuse |
|
|
Kondou
|
  |
| Joined: 05 Aug 2012 |
| Total Posts: 1148 |
|
|
| 01 Sep 2013 05:54 PM |
This one from Zach worked fine
t = {1,2,3,4,5} for i,v in pairs(t) do table.remove(t,i) end
Thanks again.
[WIJ Sergeant] Kondou |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2013 06:04 PM |
a = {1,2,3,4}; b = a;
for i,v in pairs(a) do a[i]= nil end
b will still be {1,2,3,4};
so a = {1,2,3,4}; b = a
a = { }
b still equals {1,2,3,4};
so a = { } and for i,v in pairs(a)do a[i]=nil end are the same |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2013 06:05 PM |
> t = {1,2,3,4,5} for i,v in pairs(t) do table.remove(t,i) end print(unpack(t))
2 4
I don't see how that 'works'. |
|
|
| Report Abuse |
|
|
|
| 01 Sep 2013 06:08 PM |
| Hey Kondou, if you need any help scripting, feel free to ask me and Lordjoe on Skype. |
|
|
| Report Abuse |
|
|