local func = function(t,...) return (...) end local tbl = setmetatable({},{__index=func}) print( tbl[2] ) -- tostring replacement that assigns ids local ts,id,nid,types = tostring,{},0,{table='tbl',thread='thr',userdata='uda',['function']='func'} tostring = function(x) if not x or not types[type(x)] then return ts(x) end if not id[x] then nid=nid+1; id[x]=types[type(x)]..'.'..nid end return id[x] end local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' } table.insert(t,'six'); table.insert(t,1,'seven'); table.insert(t,4,'eight'); table.insert(t,7,'nine'); table.insert(t,10,'ten'); print( #t ) -- concat print( '-- concat tests' ) function tryconcat(t) print( table.concat(t) ) print( table.concat(t,'--') ) print( table.concat(t,',',2) ) print( table.concat(t,',',2,2) ) print( table.concat(t,',',5,2) ) end tryconcat( { "one", "two", "three", a='aaa', b='bbb', c='ccc' } ) tryconcat( { "one", "two", "three", "four", "five" } ) function tryconcat(t) print( table.concat(t) ) print( table.concat(t,'--') ) print( table.concat(t,',',2) ) end tryconcat( { a='aaa', b='bbb', c='ccc', d='ddd', e='eee' } ) tryconcat( { [501]="one", [502]="two", [503]="three", [504]="four", [505]="five" } ) tryconcat( {} ) -- print the elements of a table in a platform-independent way function eles(t,f) f = f or pairs all = {} for k,v in f(t) do table.insert( all, "["..tostring(k).."]="..tostring(v) ) end table.sort( all ) return "{"..table.concat(all,',').."}" end -- insert, maxn print( '-- insert, len tests' ) local t = { "one", "two", "three", a='aaa', b='bbb', c='ccc' } print( eles(t), #t ) table.insert(t,'six'); print( eles(t), #t ) table.insert(t,1,'seven'); print( eles(t), #t ) table.insert(t,4,'eight'); print( eles(t), #t ) table.insert(t,7,'nine'); print( eles(t), #t ) table.insert(t,10,'ten'); print( eles(t), #t ) print( '#{}', #{} ) print( '#{"a"}', #{"a"} ) print( '#{"a","b"}', #{"a","b"} ) print( '#{"a",nil}', #{"a",nil} ) print( '#{nil,nil}', #{nil,nil} ) print( '#{nil,"b"}', #{nil,"b"}==0 or #{nil,"b"}==2 ) print( '#{"a","b","c"}', #{"a","b","c"} ) print( '#{"a","b",nil}', #{"a","b",nil} ) print( '#{"a",nil,nil}', #{"a",nil,nil} ) print( '#{nil,nil,nil}', #{nil,nil,nil} ) print( '#{nil,nil,"c"}', #{nil,nil,"c"}==0 or #{nil,nil,"c"}==3 ) print( '#{nil,"b","c"}', #{nil,"b","c"}==0 or #{nil,"b","c"}==3 ) print( '#{nil,"b",nil}', #{nil,"b",nil}==0 or #{nil,"b",nil}==2 ) print( '#{"a",nil,"c"}', #{"a",nil,"c"}==1 or #{"a",nil,"c"}==3 ) -- remove print( '-- remove tests' ) t = { "one", "two", "three", "four", "five", "six", "seven", [10]="ten", a='aaa', b='bbb', c='ccc' } print( eles(t), #t ) print( 'table.remove(t)', table.remove(t) ); print( eles(t), #t ) print( 'table.remove(t,1)', table.remove(t,1) ); print( eles(t), #t ) print( 'table.remove(t,3)', table.remove(t,3) ); print( eles(t), #t ) print( 'table.remove(t,5)', table.remove(t,5) ); print( eles(t), #t ) print( 'table.remove(t,10)', table.remove(t,10) ); print( eles(t), #t ) print( 'table.remove(t,-1)', table.remove(t,-1) ); print( eles(t), #t ) print( 'table.remove(t,-1)', table.remove(t,-1) ) ; print( eles(t), #t ) -- sort print( '-- sort tests' ) function sorttest(t,f) t = (t) print( table.concat(t,'-') ) if f then table.sort(t,f) else table.sort(t) end print( table.concat(t,'-') ) end sorttest{ "one", "two", "three" } sorttest{ "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" } sorttest( { "www", "vvv", "uuu", "ttt", "sss", "zzz", "yyy", "xxx" }, function(a,b) return b