lua-users home
lua-l archive

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


On Tue, Mar 25, 2008 at 3:08 PM, RJP Computing <rjpcomputing@gmail.com> wrote:
> On Tue, Mar 25, 2008 at 2:45 PM, Petite Abeille <petite.abeille@gmail.com>
> wrote:
> > Don't let that stop you! Write your own!
> >
> > local function ForEach( aTable, aFunction )
> >     for aKey, aValue in pairs( aTable ) do
> >         aFunction( aKey, aValue )
> >     end
> > end
> >
> > ForEach( { a = 1, b = 2, c = 3 }, print )
> >
> >  > a    1
> >  > c    3
> >  > b    2
> >
> > Or something!
> >
>
> Very cool. Great idea. Of coarse. Thanks for such a simple solution.
> --
> Regards,
> Ryan

That's not quite how foreach works (but it's good enough for most
people). Here's how foreach works in Lua currently:

table.foreach(t, f)
  for k,v in pairs(t) do
    local brk = f(k, v)
    if brk ~= nil then
      return brk;
    end
  end
end

foreachi works similarly but with ipairs instead.

-- 
-Patrick Donnelly

"One of the lessons of history is that nothing is often a good thing
to do and always a clever thing to say."

-Will Durant