Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

23
LINES

< > BotCompany Repo | #517 // mergeTexts (side by side merger, include)

Lua code

get("#515") -- splitLines

-- each input can be a string or an array of lines
-- result is as string concatenated with \n
function mergeTexts(lines1, lines2)
  if type(lines1) == 'string' then lines1 = splitLines(lines1) end
  if type(lines2) == 'string' then lines2 = splitLines(lines2) end
  
  local n = math.max(#lines1, #lines2)

  local w = 0
  for i=1, #lines1 do
    w = math.max(w, #(lines1[i] or ''))
  end

  local result = {}
  for i=1, n do
    local line1 = lines1[i] or ""
    local line2 = lines2[i] or ""
    table.insert(result, line1..string.rep(" ", w-#line1).." | "..line2)
  end
  return table.concat(result, "\n")
end

Author comment

Began life as a copy of #513

test run  test run with input  download  show line numbers   

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #517
Snippet name: mergeTexts (side by side merger, include)
Eternal ID of this version: #517/1
Text MD5: a8705c22426f8ecc687ade5859d8fb96
Author: stefan
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-03-11 18:56:03
Source code size: 664 bytes / 23 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 686 / 170
Referenced in: [show references]