Skip to content

Commit f9c7e46

Browse files
HybridDogsfan5
authored andcommittedJan 4, 2020
Add table.key_value_swap (#9248)
1 parent 3ad92ba commit f9c7e46

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed
 

Diff for: ‎builtin/common/misc_helpers.lua

+9
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,15 @@ function table.insert_all(t, other)
575575
end
576576

577577

578+
function table.key_value_swap(t)
579+
local ti = {}
580+
for k,v in pairs(t) do
581+
ti[v] = k
582+
end
583+
return ti
584+
end
585+
586+
578587
--------------------------------------------------------------------------------
579588
-- mainmenu only functions
580589
--------------------------------------------------------------------------------

Diff for: ‎doc/lua_api.txt

+2
Original file line numberDiff line numberDiff line change
@@ -2902,6 +2902,8 @@ Helper functions
29022902
* `table.insert_all(table, other_table)`:
29032903
* Appends all values in `other_table` to `table` - uses `#table + 1` to
29042904
find new indices.
2905+
* `table.key_value_swap(t)`: returns a table with keys and values swapped
2906+
* If multiple keys in `t` map to the same value, the result is undefined.
29052907
* `minetest.pointed_thing_to_face_pos(placer, pointed_thing)`: returns a
29062908
position.
29072909
* returns the exact position on the surface of a pointed node

0 commit comments

Comments
 (0)
Please sign in to comment.