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

read_table v1.2

Started by Tamtam18_2, 04 May 2014 - 01:01 AM
Tamtam18_2 #1
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?
Dog #2
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
Tamtam18_2 #3
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.
Dog #4
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.
Tamtam18_2 #5
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/vLSPvH3M
Setup a 6x6 monitor before use
Tamtam18_2 #6
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/vLSPvH3M

when 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.
Lyqyd #7
Posted 09 May 2014 - 03:26 PM
Threads merged.
TheOddByte #8
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
ShadowBlade #9
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?