3
bestSword = {
  {name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0},
  {name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0},
  {name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0},
  {name = 'sss' , lvl = 55, atk = 43, npcPrice = 30000 , buyPrice = 0},
  {name = 'aaa' , lvl = 60, atk = 44, npcPrice = 30000 , buyPrice = 0},
  {name = 'qwe' , lvl = 70, atk = 46, npcPrice = 35000 , buyPrice = 0},
  {name = 'asd' , lvl = 82, atk = 48, npcPrice = 60000 , buyPrice = 0}
}

I have this table, how can I iterate from last index to first? It should break depends from lvl. I just want to show this table from the best weapon. For example if Player have level 53, then I want to show only weapons for his lvl or lower. I need to show that from the best one (at top) its why I want to iterate from the last index. Could someone help?

EDIT: Thanks for help. There is still a problem that I need this changed table later. It shows all fine but I need to buy all litems from this (changed) list later. So I must replace in some way this 2 tables. Is there any easy way to do that? I tried to remove elements from this table but it still not works.

Or its possible to make some map in Lua? It must be dynamic sized so i cant use table i guess. Something with key - value

2
  • A table is a key-value map. The way you have constructed it, the keys for the initial values are implied. See the Lua documentation for table and table that has a sequence. (Maybe post a separate question after that.) Commented Feb 7, 2016 at 14:01
  • Nice question to ... and welcome to "upvote level" ;-)
    – GhostCat
    Commented Sep 8, 2017 at 10:49

2 Answers 2

11

A numeric for loop, counting down, is the best option:

local t = {2,4,6,8}

for i = #t, 1, -1 do
    print(t[i])
end
0
1

Assuming that the table is not necessarily sorted into level order (unlike the example), we need to do two things:

  • Find which swords are in the level range
  • Sort them into descending order

Now the first one in the temporary table is the "best" sword.

Like this:

bestSword = {
  {name = 'www' , lvl = 35, atk = 38, npcPrice = 15000 , buyPrice = 0},
  {name = 'bbb' , lvl = 40, atk = 40, npcPrice = 20000 , buyPrice = 0},
  {name = 'eee' , lvl = 50, atk = 42, npcPrice = 25000 , buyPrice = 0},
  {name = 'sss' , lvl = 55, atk = 43, npcPrice = 30000 , buyPrice = 0},
  {name = 'aaa' , lvl = 60, atk = 44, npcPrice = 30000 , buyPrice = 0},
  {name = 'qwe' , lvl = 70, atk = 46, npcPrice = 35000 , buyPrice = 0},
  {name = 'asd' , lvl = 82, atk = 48, npcPrice = 60000 , buyPrice = 0}
}

myLevel = 53  -- wanted level

-- temporary table
possible = { }

-- extract ones which are in range
for k, v in ipairs (bestSword) do
  if v.lvl <= myLevel then
    table.insert (possible, v)
  end -- if
end -- for

if #possible == 0 then
  print "No matching swords"
else
  table.sort (possible, function (a, b) return a.atk > b.atk end )
  bestSword = possible [1]
  print ("Best sword is", bestSword.name, "lvl =", bestSword.lvl,
         "atk = ", bestSword.atk)
end -- if

Or its possible to make some map in Lua? It must be dynamic sized so i cant use table i guess. Something with key - value

Tables in Lua are maps. Every table has key/value pairs. The one you are using there are simply numerically-keyed tables.

All tables are dynamically sized.

2
  • Im almost sure its why i need, let me check :D btw. what is size of possible = {} at start? in Lua is some kind of dynamic tables or it just creates new one when i instert / remove values?
    – minizibi
    Commented Feb 6, 2016 at 21:40
  • In that case please accept my answer, so that Stack Overflow recognizes it as the accepted answer (you can change your accepted answer). Commented Feb 6, 2016 at 22:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.