lua-users home
lua-l archive

Re: How to break out of nested loops

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> I found myself using this trick very often but I have no idea what
> price in terms of performance and memory consumption it costs
> (creating closures just for a single use, etc). Is it a good idiom and
> are there any alternatives??
You can also use a 'do' block:
--breaking from nested loops
t={ {0,1,2,3,4}, {10,11,-12,13,14} } -- two dimensional array
do
 for i=1,#t do
 for j=1,#t[i] do
 if t[i][j]<0 then return end --break out of all loops if negative
 print(i,j,t[i][j])
 end
 end
end
It might be not be cheaper, but it looks better IMHO. :)
Cheers,
Luis.
-- 
A mathematician is a device for turning coffee into theorems.
 -- P. Erdos 
-- 
Luis Carvalho
Applied Math PhD Student - Brown University
PGP Key: E820854A <carvalho@dam.brown.edu>

AltStyle によって変換されたページ (->オリジナル) /