lua-users home
lua-l archive

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


Hi Tony,

> -- globals
> true = 0
> false = -1
> length = 0
> 
> -- print_item and count items in list
> function print_item(i,x)
> 	print (i,x)
> 	length = length + 1 -- I don;t like doing this
> end
> 
> -- check_item
> function check_item (a,b)
> 	a = strlower(a)
> 	b = strlower(b)
> 	a = tostring (a)
> 	b = tostring (b)
> 	return a, b
> end
> 
> -- sort_list
> function sort_list (list,n)
> 	local sorted = true
> 	while sorted == true do
> 		sorted = false
> 		for i=1,n-1,1 do
> 			a, b = check_item (list[i],list[i+1])
> 			if a>b then
> 				list[i],list[i+1] = 
> list[i+1],list[i] -- swap
> 				sorted = true
> 			end
> 		end
> 	end
> end
> 
> -- list = {3,7,4,2.1,3.1416,9,-1,0}
> -- list = {"cat","dog","bird","shark","bat","mouse"}
> local list = {"cat",7,"ant",4,2,"bird","Bat","Duck"}
> 
> foreachi (list, print_item)
> sort_list (list,length)
> print ("Sorted list:")
> foreachi (list, print_item)



local list = {"cat",7,"ant",4,2,"bird","Bat","Duck"}
foreach(list, print) -- or foreach(list,function(k,v) print(v) end) -- for
just values
sort(list,function(a,b) return tostring(a)<tostring(b) end)
print ("Sorted list:")
foreach(list, print)

file:///W:/Libs/lua/doc/manual.html#6.2
or you want some list "classes" have a look at:
http://lua-users.org/wiki/PythonLists

Nick