lua-users home
lua-l archive

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

Sorry for the noise. I noticed some cut and paste errors and I thought
I'd turn it into an actual iterator...

#!/usr/bin/env lua

local big =
 { foo = { "sub1_foo","sub2_foo"  },
   bar = { "sub1_bar", "sub2_bar" }

function iter (t)
	local k, v,sk, sv
	-- can't use syntax of 'iter_func = function()
	-- unless you declare 'local iter_func()' prior
    local function iter_func()
		if type(sk) == "nil" then
			--check for next outer-element.
			k, v = next(t, k)
			if k then --if no outer, then we're done.
				sk, sv = next(v, sk)
		--< Why I didn't do an anonymous funciton...
				return iter_func()
			local old_k, old_sv = k, sv
			sk, sv = next(v, sk)
			return old_k, old_sv
		-- we're done iterating here.
		return nil

	--at this point, returning iter_func
	--non-locals are k, v, sk, sv and iter_func
	return iter_func

for k, v in iter(big) do
	print(k.. " -> "..v)


Okay... back to work... :)