Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
InsuperablE

Geolyzer and 4 tables.

Question

I'm a newbie in OpenComputers mod.

I have 4 tables with data.
How do I glue the tables into one so that they go in this order, how can I do this?
Make it look like they are superimposed on each other.
And is it possible to translate the resulting table into a two-dimensional array?

 

1234.png

1.png

Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 0

Hey,

here is a function that can glue multiple tables together. It will work for your 2x2 setup, but is more flexible.

--# x: number of tables on (horizontal) x-axis (2 in your case)
--# y: number of tables on (vertical) y-axis (2 in your case)
--# step: number of continuing elements from 1 table (8 in your case)
--# ... : the tables you want to merge
local function merge(x, y, step, ...)
  local res = {}

  for i, tbl in ipairs{...} do
    local hy = (i - 1) // x
    local hx = (i - 1) % x
    for j = 1, #tbl, step do
      table.move(tbl, j, j+step-1, hy * x * #tbl + hx * step + 1, res)
      hx = hx + x
    end
  end

  --# if you merge tables in only one specific way, you can move these parameters out of the function
  --# or hardcode their values
  
  local x = 3
local y = 3
local step = 3

local function merge_1(...)
  local res = {}

  for i, tbl in ipairs{...} do
    local hy = (i - 1) // x
    local hx = (i - 1) % x
    for j = 1, #tbl, step do
      table.move(tbl, j, j+step-1, hy *x* #tbl + hx * step + 1, res)
      hx = hx + x
    end
  end

  return res
end
  return res
end

This function however requires Lua 5.3 to run (which I would always recommend). If you need Lua 5.2, here's a modified version:

local function copy(t1, s, e, j, t2)
  local c = 0
  for i = s, e do
    t2[j+c] = t1[i]
    c = c + 1
  end
end

local function merge(x, y, step, ...)
  local res = {}

  for i, tbl in ipairs{...} do
    local hy = math.floor((i - 1) / x)
    local hx = (i - 1) % x
    for j = 1, #tbl, step do
      copy(tbl, j, j+step-1, hy * x * #tbl + hx * step + 1, res)
      hx = hx + x
    end
  end

  return res
end

Here's a little test:

--[[
 Merging 9 tables:
 81 80 79 | 78 77 76 | 75 74 73
 72 71 70 | 69 68 67 | 66 65 64
 63 62 61 | 60 59 58 | 57 56 55
 ------------------------------
 54 53 52 | 51 50 49 | 48 47 46
 45 44 43 | 42 41 40 | 39 38 37
 36 35 34 | 33 32 31 | 30 29 28
 ------------------------------
 27 26 25 | 24 23 22 | 21 20 19
 18 17 16 | 15 14 13 | 12 11 10
 09 08 07 | 06 05 04 / 03 92 01
]]

local t1 = {1, 2, 3, 10, 11, 12, 19, 20, 21}
local t2 = {4, 5, 6, 13, 14, 15, 22, 23, 24}
local t3 = {7, 8, 9, 16, 17, 18, 25, 26, 27}
local t4 = {28, 29, 30, 37, 38, 39, 46, 47, 48}
local t5 = {31, 32, 33, 40, 41, 42, 49, 50, 51}
local t6 = {34, 35, 36, 43, 44, 45, 52, 53, 54}
local t7 = {55, 56, 57, 65, 65, 66, 73, 74, 75}
local t8 = {58, 59, 60, 67, 68, 69, 76, 77, 78}
local t9 = {61, 62, 63, 70, 71, 72, 79, 80, 81}

local res = merge(3, 3, 3, t1, t2, t3, t4, t5, t6, t7, t8, t9)

for i, v in ipairs(res) do
  print(v)
end
--# This will print numbers 1-81 in order

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.