Programming in Lua - 4.3.5
Programming in Lua - 4.3.5
Programming in Lua
Part I. The Language
Chapter 4. Statements
The generic loop shares two properties with the numeric loop:
The loop
variables are local to the loop body
and you should never assign any value to
the loop variables.
Now you want to translate a name into its position in the week.
You can
search the table, looking for the given name.
Frequently, however,
a more
efficient approach in Lua is to build a reverse table,
say revDays,
that has the
names as indices and the numbers as values.
That table would look like this:
revDays = {["Sunday"] = 1, ["Monday"] = 2,
["Tuesday"] = 3, ["Wednesday"] = 4,
["Thursday"] = 5, ["Friday"] = 6,
["Saturday"] = 7}
x = "Tuesday"
print(revDays[x]) --> 3
revDays = {}
revDays[v] = i
end