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

table api and getting tables from other programs

Started by pinksheep00, 23 March 2013 - 07:39 PM
pinksheep00 #1
Posted 23 March 2013 - 08:39 PM
How can i access tables from other programs, by checking for all the program that has the table called fileList then
use those tables?

And I know I already ask this question but i still don't understand how it completely works. :/
its about the table.sort() function and using a comparison function
like sorting the sub tables by comparing their sub tables (sub tableception) that are either number or string

for example i want to arrange a table by putting first the active status, then arrange all of those sub table with
active status starting from the highest priority to lowest. second the inactive status, then start again from the highest prority to lowest.

the table

local fileList = {
{name = "file_01", status = "inactive", priority = 1},
{name = "file_02", status = "active", priority = 1}
{name = "file_03", status = "active", priority = 2},
{name = "file_04", status = "active", priority = 0}
{name = "file_05", status = "inactive", priority = 0}
}

can anyone explain this for me? please :)/>
pinksheep00 #2
Posted 24 March 2013 - 04:26 AM
anyone?
theoriginalbit #3
Posted 24 March 2013 - 04:33 AM
well it is a little difficult, given some circumstances you can use os.loadAPI to load the file and then check for the existence of the table. but it can cause problems if the program contains infinite loops and such.

as for the sort, you can pass a custom function to the table.sort function that does the comparisons for you.

local function compare( a, b )
  return a.priority> b.priority and a.status > b.status
end

table.sort( fileList, compare )
See edit…
this should sort it where its available first, in priority order, and then inactive in priority order.
when using > or < on strings Lua does something cool that most other languages cant/dont do. it gets the ASCII representation (byte) of each character and compares them, the minute one is greater or smaller (which ever operator you use) it will return a boolean with the result of whether it is > or <

EDIT: Whoops, bug… fix:

local function compare( a, b )
  return a.status..a.priority < b.status..b.priority
end

table.sort( fileList, compare )
note that we are concatenating the status and priority together to do the comparison, its all to do with how the strings are checked, out of 'active1' and 'active2', 'active2' is the bigger one (2 is after 1). out of 'active2' and 'inactive1', 'inactive1' is the bigger one (i is after a)
Edited on 24 March 2013 - 04:29 AM
pinksheep00 #4
Posted 24 March 2013 - 05:26 AM
thank you so much for helping me sir :D/>
theoriginalbit #5
Posted 24 March 2013 - 05:28 AM
no problems :)/>