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

26
LINES

< > BotCompany Repo | #41 // copy attributes around in HTML sources

Lua code

function solution(input, cmd)
  -- parse all <name>="<bla>" (key/value pairs)
  local i, values, pos = 0, {}, {}
  local j, k, v
  while true do
    j, i, k, v = string.find(input, '([a-zA-Z]+)="([^"]*)"', i+1)
    if not j then break end
    values[k] = v
    pos[k] = {j+#k+2, i}
  end

  -- cmd = "copy <key1> to <key2>"
  local key1, key2 = cmd:match("copy ([%S]+) to ([%S]+)")
  if not key1 then error("Could not parse command: "..cmd) end

  -- look up key1
  local value = values[key1]
  if not value then error("attribute "..key1.." not found") end

  -- paste after key2 in input (removing the old value)
  if not pos[key2] then error("attribute "..key2.." not found") end
  local i, j = unpack(pos[key2])
  input = input:sub(1, i-1)..value..input:sub(j)

  return input
end

test run  test run with input  download  show line numbers   

Relations

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

No comments. add comment

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 808 [visualize]

Snippet ID: #41
Snippet name: copy attributes around in HTML sources
Eternal ID of this version: #41/1
Text MD5: e624ef718ed08d9834a33bdd6931b29a
Author: stefan
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2013-12-22 13:19:22
Source code size: 808 bytes / 26 lines
Pitched / IR pitched: Yes / Yes
Views / Downloads: 1458 / 368
Referenced in: [show references]