Jump to content




[lua] [Solved] Table advanced question


  • You cannot reply to this topic
8 replies to this topic

#1 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 15 October 2012 - 09: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

#2 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 15 October 2012 - 09: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))


#3 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 15 October 2012 - 09:45 PM

View Postluanub, on 15 October 2012 - 09:30 PM, said:

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

#4 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 15 October 2012 - 10: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

#5 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 15 October 2012 - 10: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.

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 15 October 2012 - 10: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 ™ 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.

#7 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 15 October 2012 - 11:29 PM

View PostLyqyd, on 15 October 2012 - 10:53 PM, said:

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/>

#8 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 15 October 2012 - 11:43 PM

Try using textutils.serialize and textutils.unserialize to pass tables through rednet. Check the wiki for more info.

#9 darkrising

  • Members
  • 234 posts
  • LocationScotland

Posted 16 October 2012 - 12: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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users