lua table.sort crashes with "attempt to compare nil with string"

Viewed 429

I am trying to use lua on an embedded linux device. table.sort gives a wired error attempt to compare nil with string. I tied exactly this code on my local lua interpreter and it worked. What could cause such a behavior? The embedded lua is compiled through yocto.

# lua
Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> a={}
> a[#a+1]="foo"
> a[#a+1]="baar"
> a[#a+1]="baar_1"
> a[#a+1]="baar_2"
> print(a[0])
nil
> print(#a)  
4
> for i=0,#a do print (a[i]) 
>> end
nil
foo
baar
baar_1
baar_2
> table.sort(a)
attempt to compare nil with string
stack traceback:
        [C]: in function 'table.sort'
        stdin:1: in main chunk
        [C]: in ?
> table.sort(a, function(x,y) print(x); print(y); return x < y end)
foo
baar_2
nil
baar_2
stdin:1: attempt to compare nil with string
stack traceback:
        stdin:1: in function <stdin:1>
        [C]: in function 'table.sort'
        stdin:1: in main chunk
        [C]: in ?
>  for key, val in pairs(a) do print(key, val) end
1       foo
2       baar
3       baar_1
4       baar_2
> 
  • (update #1 added print)
  • (update #2 added print to table.sort)
  • (update #3 added loop over key/val)
0 Answers
Related