Programming-Idioms

History of Idiom 43 > diff from v31 to v32

Edit summary for version 32 :
New Lua implementation by user [Nepta]

Version 31

2016-02-19, 21:39:41

Version 32

2016-04-07, 09:54:07

Idiom #43 Break outer loop

Look for a negative value v in 2D integer matrix m. Print it and stop searching.

Idiom #43 Break outer loop

Look for a negative value v in 2D integer matrix m. Print it and stop searching.

Code
for i,v1 in ipairs(m) do
   for j,v2 in ipairs(v1) do
      if v2 < 0 then
         print(v2)
         state = "found"
         goto outer
      end
   end
end
::outer::
Comments bubble
you need at least Lua5.2.

using a function might be a better idea than using a goto,
as an example see http://www.programming-idioms.org/idiom/20/return-two-values/1661/lua