Continue your loops in lua

I am so used to the continue word in Java and C that I was surprised to see it absent in lua.

It is however relatively easy to implement it with a good ol’ goto.

Here is a (rather stupid) example. We get a directory listing and we filter out the hidden files (starting with a dot):

local h = io.popen("ls -a")
for line in h:lines() do
   if string.match(line, "^%.") then goto continue end
   print(line)
   ::continue::
end
h:close()

 

Avatar
Lydéric Landry
écrivain et éditeur

Après plusieurs vies, j'écris maintenant