lua-users home
lua-l archive

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


>In this mode, lua would read its input record by record (lines by
>default) and perform various actions based on a pattern (including
>address) match. Has anyone added an implicit record traversal loop
>and matching mechanism like this to lua?

Here is a start...
--lhf

function split(s)
 local t={[0]=s,n=0}
 gsub(s,"%s*(%S+)",function(x) tinsert(%t,x) end)
 return t
end

function AWK(f)
 if type("BEGIN")=="function" then BEGIN() end
 while 1 do
  local l=read()
  if l==nil then break end
  f(split(l))
 end
 if type("END")=="function" then END() end
end

AWK(function (t) foreach(t,print) end)