RosettaCodeData/Task/Associative-array-Creation/Pluto/associative-array-creation....

26 lines
531 B
Plaintext

-- Create a map of key, value pairs.
local planets = {
Mercury = 1,
Venus = 2,
Earth = 3,
Mars = 4,
Jupiter = 5,
Saturn = 6,
Uranus = 7,
Neptune = 8,
Pluto = 9
}
-- Print it
-- Note that order is not guaranteed.
for k, v in planets do print(k, v) end
print()
-- Create a new one by inverting the key, value pairs.
local planets2 = planets:invert()
-- Print it.
-- Now treated as an array with indices 1 to 9
-- which guarantees order.
for k, v in planets2 do print($"{k} {v}") end