This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
darkrising's profile picture

[lua] [Solved] Table advanced question

Started by darkrising, 15 October 2012 - 08:24 PM
darkrising #1
Posted 15 October 2012 - 10:24 PM
I'm trying to get a function to work that returns a string list of all the data in a table, my question is: how can i get the length of the array from a passed string name of the array?

here is the code:



function displayArray(ARRAY)
local list = ""  
  for i=1, #[ARRAY] do -- this line is the problem, i need to get an array length of an array as a passed string
	if (i == 1) then
	  list = (list..[ARRAY][i])
	else
	  list = (list..", "..[ARRAY][i])
	end
  end
return list
end
NameOfArray = {"dog", "cat", "fish", "something more pointless"}

print(displayArray("NameOfArray"))

I have tried table.getn(t) but it keeps moaning about it being a string and not a table
Luanub #2
Posted 15 October 2012 - 10:30 PM
try doing just #ARRAY

table.maxn() should work as well. Are you sure it is getting passed as a table? Do this to check

print("ARRAY is type "..type(ARRAY))
darkrising #3
Posted 15 October 2012 - 10:45 PM
try doing just #ARRAY

table.maxn() should work as well. Are you sure it is getting passed as a table? Do this to check

print("ARRAY is type "..type(ARRAY))

its being passed as a string, i just want the string as a reference to the table if that's possible like above
darkrising #4
Posted 15 October 2012 - 11:02 PM
Well, on later testing passing it as a table works:

function displayArray(ARRAY)
local list = ""  
  for i=1, #ARRAY do
		if (i == 1) then
		  list = (list..ARRAY[i])
		else
		  list = (list..", "..ARRAY[i])
		end
  end
return list
end
NameOfArray = {"dog", "cat", "fish", "something more pointless"}

print(displayArray(NameOfArray))

But i'm still looking for a way to pass a string instead
Kingdaro #5
Posted 15 October 2012 - 11:48 PM
If your array is global, you can access it using _G.

function displayArray(arrayname)
  local list = _G[arrayname]
  -- your code here
end

print(displayArray 'NameOfArray')
I'm not sure how to do it if your array is local, though. Maybe there's an _L table or something.
Lyqyd #6
Posted 15 October 2012 - 11:53 PM
You're much better off passing a table. Why do you wish it to pass a string?

As a side note, Lua compiles to byte code before being executed, so unless the table is accessible without passing it, the function won't be able to reach it by name anyway.

If you really HAVE to reference it by a string containing its name (which is a Bad Idea (TM) and is bad programming practice) instead of by reference to the table itself, you could store a table in _G, then put the tables you want to pass by reference in the table in _G, then pass the string and reference them via _G.tablesByReference[string].value

But that's a hellish way to misuse tables.
darkrising #7
Posted 16 October 2012 - 12:29 AM
You're much better off passing a table. Why do you wish it to pass a string?

As a side note, Lua compiles to byte code before being executed, so unless the table is accessible without passing it, the function won't be able to reach it by name anyway.

If you really HAVE to reference it by a string containing its name (which is a Bad Idea ™ and is bad programming practice) instead of by reference to the table itself, you could store a table in _G, then put the tables you want to pass by reference in the table in _G, then pass the string and reference them via _G.tablesByReference[string].value

But that's a hellish way to misuse tables.

As rednet returns strings, I was trying to use one function instead of one for each table but I guess I could build just one table for all the tables instead of having separate ones, but thanks for the info :D/>/>
MysticT #8
Posted 16 October 2012 - 12:43 AM
Try using textutils.serialize and textutils.unserialize to pass tables through rednet. Check the wiki for more info.
darkrising #9
Posted 16 October 2012 - 01:38 AM
I'm not too happy with the way textutils.serialize works so I wrote my own split and join functions so I could send more data at once