Hacker News new | ask | show | jobs
by kungfooman 3755 days ago
Terra is on my radar for a long time, but the Lua syntax prevents me every time to actually use it.

Basically making a simple foreach loop in Lua escalates everytime into a hour trial-and-error session.

1 comments

Here:

    for var_1, ยทยทยท, var_n in explist do block end
use `pairs(a_table)` to get `key, val` variables bound respectively. Use `ipairs` to get a loop over indices and values. This is very similar to Python, only with "do/end" instead of indent/dedent. What you can't do in Python, but can in Lua is to provide less variable names than the iterator function returns. For example, to get a list of keys when iterating over `(key, val)` pairs in Python you'd have to do this:

    for key, _ in some_dict.items()
While in Lua it's less explicit:

    for key in pairs(some_table)
...and that's it.