24 posts
Posted 04 May 2014 - 03:01 AM
local m = peripheral.wrap("right")
local sensor = peripheral.wrap("top")
local players = sensor.getPlayerNames() -- This can be any function. The Target function to get table information.
m.setCursorPos(1,1)
-- added code for this function
for a, name in pairs(players) do
print(name)
t = sensor.getPlayerData(name) -- t for table since table is taken
-- Main Code
for key, value in pairs(t) do
for x, y in pairs(value) do -- if there are more nested tables copy, paste and edit key and values for this for statement as many times as needed
m.write(tostring(x)..": "..tostring(y))
m.setCursorPos(1,x+1)
if x > 40 then
m.setCursorPos(17,x+1)
end
end
end
end
Its a simple code really. Its designed to look into tables provided by such functions as getTankInfo() and getPlayerData(). My problem is that after table 40 a boolean is presented and the code breaks: 'test:13: bad argument: table expected, got boolean. How would I go about this error? I think I need to do a separate if then else statement to read tables 1-40 and then in the else read booleans after 40 tables. what are your guy's thoughts?
1220 posts
Location
Earth orbit
Posted 04 May 2014 - 04:11 AM
I *think* this is what you're looking for…
if type(t) == "table" then
** do your loop **
else
** it's not a table **
end
replace 't' with 'value' or 'y' or whichever returned value you're having trouble with
Edited on 04 May 2014 - 02:13 AM
24 posts
Posted 04 May 2014 - 05:37 AM
-- added code for this function
for a, name in pairs(players) do
print(name)
t = sensor.getPlayerData(name) -- t for table since table is taken
-- Main Code
-- if there are more nested tables copy, paste and edit key and value
-- for this for statement as many times as needed
for key, value in pairs(t) do
for x, y in pairs(value) do
if x < 41 then
m.setCursorPos(1,x)
else
m.setCursorPos(21,x-39)
end
if x < 41 then
m.write(tostring(x)..": "..tostring(y))
else
m.write(x ..": ".. y) -- value and tostring(value) also do not work
end
end
end
end
if I do it like this (or as you suggested dog) it still gives me the same error. Looking into the nbt data for the player might give me a clue as to what to do next here. Also, it gives me a second table 40 before with a different string just before the code breaks.
1220 posts
Location
Earth orbit
Posted 06 May 2014 - 07:02 AM
Apologies for the late reply. Please post your code showing how you used 'type' to identify the table. I'd like to see what you tried.
24 posts
Posted 09 May 2014 - 08:26 AM
This may get split into a different topic at this point in my interest, but i created this part of read_table to try to get to the variables postion for a player to make sure I wasn't getting another peice of code in another program wrong. pd (my player detector) has been fixed so read_table is obsolete for now. my time is short so I won't have time to return to read_table until later, but! I'll just post my code here for people to see and use as they like.
Read_table pd version:
http://pastebin.com/vLSPvH3MSetup a 6x6 monitor before use
24 posts
Posted 09 May 2014 - 08:52 AM
I've moddified my read_table for the specific use of looking into what a golden ticket has and I'm stumped at one bit:
http://pastebin.com/vLSPvH3Mwhen the program is run line 6 in the output doesn't appear. I've doubled checked my code and everything should be fine except when a is equal to 6 it doesn't want to run the for loop. I've added in "print("(".. a ..")") at the last minute just to verify this and it's printing (6) but not wanting to do the latter.
Line 6, in the output, contains a tabled defined by ench. I want to know what that is, but I can't get into it's table.
8543 posts
Posted 09 May 2014 - 03:26 PM
Threads merged.
1852 posts
Location
Sweden
Posted 09 May 2014 - 05:03 PM
I'm not sure if you fixed the first problem or not, But test and see if this works
Before you run this loop
for x, y in pairs( value ) do
...
end
test doing this
if type( value ) == "table" then
for x, y in pairs( value ) do
...
end
end
And for the second problem, try changing this loop from pairs
for a, name in pairs( players ) do
...
end
to ipairs
for a, name in ipairs( players ) do
...
end
1 posts
Posted 09 May 2014 - 07:36 PM
My problem is that after table 40 a boolean is presented and the code breaks: 'test:13: bad argument: table expected, got boolean.
You're getting that error because you're trying to pass a boolean to pairs(). Instead of manually adding extra loops you could use a recursive function similar to this.
function printTable(t)
for k, v in pairs(t) do
if(type(v) == "table") then
print(k)
printTable(v)
else
print(k .. tostring(v))
end
end
end
(I removed stuff that's in the pastebin for readability so don't copypaste. Shouldn't be doing that anyway though ;)/>)
pastebin get vYteUqWG shows it working. Shouldn't cause a stack overflow unless your table's depth is > 256 so should be plenty. What's the max table depth anyway?
when the program is run line 6 in the output doesn't appear.
Seems to show up fine for me. I only get the table expected error later. You sure that's the correct pastebin?