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

1  
function solution(input, cmd)
2  
  -- parse all <name>="<bla>" (key/value pairs)
3  
  local i, values, pos = 0, {}, {}
4  
  local j, k, v
5  
  while true do
6  
    j, i, k, v = string.find(input, '([a-zA-Z]+)="([^"]*)"', i+1)
7  
    if not j then break end
8  
    values[k] = v
9  
    pos[k] = {j+#k+2, i}
10  
  end
11  
12  
  -- cmd = "copy <key1> to <key2>"
13  
  local key1, key2 = cmd:match("copy ([%S]+) to ([%S]+)")
14  
  if not key1 then error("Could not parse command: "..cmd) end
15  
16  
  -- look up key1
17  
  local value = values[key1]
18  
  if not value then error("attribute "..key1.." not found") end
19  
20  
  -- paste after key2 in input (removing the old value)
21  
  if not pos[key2] then error("attribute "..key2.." not found") end
22  
  local i, j = unpack(pos[key2])
23  
  input = input:sub(1, i-1)..value..input:sub(j)
24  
25  
  return input
26  
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: 1459 / 368
Referenced in: [show references]