lua-users home
lua-l archive

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


Duh.  Thanks, Rici.

Funny how web blame a language when we screw up, huh?
I had a printTable() at the end of my loop without the test to make sure something was in it.

Thanks!
Mike

Rici Lake wrote:

On 21-Oct-04, at 10:22 PM, Mike Crowe wrote:

I see I have to declare function before using.

No you don't, unless you are writing mutually recursive local functions.

However, it appears that the function executes once as the system runs.  I had to put in the statement below "if tab[1] then" to catch the first run with a nil.  Shouldn't the function only been called when I call it below?

Yes, so I have no idea what you are doing to cause it to run.

I can only imagine what you are doing, but here's an easy sample:

local function printTable(tab)
  for i, val in ipairs(tab) do
    print("  Token "..i..": "..val)
  end
end

-- This just splits the line on whitespace --
local function parseLine(line)
  local t = {}
  for w in string.gfind(line, "([%S]+)") do
    table.insert(t, w)
  end
  return t
end

-- Test run on this very program
local lineno = 1
for line in io.lines("test.lua") do
  local t = parseLine(line)
  print("Line "..lineno)
  printTable(t)
  lineno = lineno + 1
end


-- Partial output (try it yourself):
rlake@freeb:~$ lua test.lua
Line 1
  Token 1: local
  Token 2: function
  Token 3: printTable(tab)
Line 2
  Token 1: for
  Token 2: i,
  Token 3: val
  Token 4: in
  Token 5: ipairs(tab)
  Token 6: do
Line 3
  Token 1: print("
  Token 2: Token
  Token 3: "..i..":
  Token 4: "..val)
Line 4
  Token 1: end
Line 5
  Token 1: end



--
Signature
In Him, Mike
Fish