200 posts
Location
Scotland
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
1111 posts
Location
Portland OR
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))
200 posts
Location
Scotland
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
200 posts
Location
Scotland
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
1688 posts
Location
'MURICA
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.
8543 posts
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.
200 posts
Location
Scotland
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/>/>
1604 posts
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.
200 posts
Location
Scotland
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