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()

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s