Lua - tableの扱い

新しくプログラミング言語を覚えたいときに行うべき10の練習問題 | IDEA*IDEA
練習問題3をやりました、と思ったら最大値と最小値求めていなかったorz
明日手直しー。

function compare(x, y)
   if(x.weight < y.weight) then                                                                                                                                               
      return 1
   end
end

function show_all(list)
   for i=1,(# list) do                                                                                                                   
      print(" "..list[i].name, list[i].weight)
   end
end

function main_loop()
   local name local weight
   local list = {}

   while true do
      io.write("Input name >>> ")
      name = io.read()                                                                                                                   

      io.write("Input weight >>> ")
      weight = io.read()                                                                                                                           

      table.insert(list,{["name"]=name, ["weight"]=tonumber(weight)})
      table.sort(list,compare)
      show_all(list)
   end
end

main_loop()