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

25
LINES

< > BotCompany Repo | #349 // Table functions (valuestokeys, keystolist, all)

Lua code

1  
function valuestokeys(tbl)
2  
  local tbl2 = {}
3  
  for _, v in pairs(tbl) do
4  
    tbl2[v] = true
5  
  end
6  
  return tbl2
7  
end
8  
9  
function keystolist(tbl)
10  
  local tbl2 = {}
11  
  for k, _ in pairs(tbl) do
12  
    table.insert(tbl2, k)
13  
  end
14  
  return tbl2
15  
end
16  
17  
-- iterate over values of an array
18  
function all(t)
19  
  local i = 0
20  
  local n = #t
21  
  return function ()
22  
    i = i + 1
23  
    if i <= n then return t[i] end
24  
  end
25  
end

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: #349
Snippet name: Table functions (valuestokeys, keystolist, all)
Eternal ID of this version: #349/1
Text MD5: 3504c79f98d8c271dcd27bf7d4acc341
Author: stefan
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-03-13 16:57:37
Source code size: 422 bytes / 25 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 761 / 209
Referenced in: [show references]