Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bidirectional Map in Lua

Tags:

lua

How would you go about making a bidirectional map in Lua? I am new to Lua, and the only way that I can think of is to put every key-value pair in a table, iterate over each pair to add them (key-value swapped) to the original table.

Is there a better way to do this?

like image 240
Graznarak Avatar asked Jul 11 '26 17:07

Graznarak


1 Answers

I don't think there is a better way (assuming you can handle key/value conflicts). A slightly better way may be to store swapped values when you add a new values to the table (or update/remove an existing value), but the overall idea is the same.

like image 62
Paul Kulchenko Avatar answered Jul 18 '26 07:07

Paul Kulchenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!