[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Why this error in table.sort()?
- From: Steve Litt <slitt@...>
- Date: Fri, 4 Feb 2011 00:49:29 -0500
Hi all,
For my presentation, I'm using table.sort(mytable, sortfcn), where mytable has
only consecutive integer keys starting with 1, and each element consists of a
table with named keys (fname, lname, from, to). I want to sort by lname then
fname.
By mistake I changed the first line of the sort order function's if statement
from:
if thiss.lname < nextt.lname then return true
to
if thiss.lname < nextt.fname then return true
Notice nextt.fname. That's not my intent, and I understand it should give me
wrong results, but why should it be a compile time error? Whereas nextt.lname
runs as expected, nextt.fname throws the following error:
=================
slitt@mydesk:/d/at/htmlslides_data/leap_lua$ ./tableproblem.lua
/usr/bin/lua: ./tableproblem.lua:20: attempt to index local 'thiss' (a nil
value)
stack traceback:
./tableproblem.lua:20: in function <./tableproblem.lua:14>
[C]: in function 'sort'
./tableproblem.lua:28: in main chunk
[C]: ?
slitt@mydesk:/d/at/htmlslides_data/leap_lua$
=================
Ughhh!
I've attached the program, and I'm printing it inline for your convenience:
#!/usr/bin/lua
local p=print
local sf=string.format
local ts=tostring
-- INITIALIZE presidents with TABLE WITH LAST 4 PRESIDENTS
local presidents = {}
table.insert(presidents,{lname="Obama",fname="Barack",from=2009,to=nil})
table.insert(presidents,{lname="Bush",fname="George W",from=2001,to=2008})
table.insert(presidents,{lname="Clinton",fname="Bill",from=1993,to=2000})
table.insert(presidents,{lname="Bush",fname="George HW",from=1989,to=1992})
-- SORT ORDER FUNCTION byname
byname = function (thiss, nextt)
-- Following line has deliberate mistake nextt.fname
-- instead of nextt.lname like it should be
-- Why does it throw a compile error with this mistake?
-- Change nextt.fname to nextt.lname and the error vanishes.
if thiss.lname < nextt.fname then return true
elseif thiss.lname > nextt.lname then return false
elseif thiss.fname < nextt.fname then return true
else return false
end
end
-- SORT BY NAME AND PRINT
table.sort(presidents, byname)
for k, v in ipairs(presidents) do
print(sf("Record %d: %s %s served from %s until %s",
k, v.fname, v.lname, ts(v.from), ts(v.to)))
end
#!/usr/bin/lua
local p=print
local sf=string.format
local ts=tostring
-- INITIALIZE presidents with TABLE WITH LAST 4 PRESIDENTS
local presidents = {}
table.insert(presidents,{lname="Obama",fname="Barack",from=2009,to=nil})
table.insert(presidents,{lname="Bush",fname="George W",from=2001,to=2008})
table.insert(presidents,{lname="Clinton",fname="Bill",from=1993,to=2000})
table.insert(presidents,{lname="Bush",fname="George HW",from=1989,to=1992})
-- SORT ORDER FUNCTION byname
byname = function (thiss, nextt)
-- Following line has deliberate mistake nextt.fname
-- instead of nextt.lname like it should be
-- Why does it throw a compile error with this mistake?
-- Change nextt.fname to nextt.lname and the error vanishes.
if thiss.lname < nextt.fname then return true
elseif thiss.lname > nextt.lname then return false
elseif thiss.fname < nextt.fname then return true
else return false
end
end
-- SORT BY NAME AND PRINT
table.sort(presidents, byname)
for k, v in ipairs(presidents) do
print(sf("Record %d: %s %s served from %s until %s",
k, v.fname, v.lname, ts(v.from), ts(v.to)))
end