• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • 2026 staff recruitment is open! Check it out and consider applying!

Re-ordering an LUA table

Leo32

Getting back into it...
Joined
Sep 21, 2007
Messages
990
Solutions
14
Reaction score
551
Hey Guys,

I'm getting a little stuck here;
What I'm after seems simple enough but I'm not familiar enough with tables to get my head around it:

Code:
    for _, v in pairs(self:getLoot()) do
       local itemList = self:createLootItem(v, modifier)
       if itemList then
         for _, item in ipairs(itemList) do
           print(item:getName()) -- this is the data I would like to re-order.
           if item:isContainer() then
             local lootContainer = self:createLootContainer(item, v, modifier)
             if lootContainer then
               corpse:addItemEx(item)
             end
           else
             corpse:addItemEx(item, 1)
           end
         end
       end
     end

Example output of print line above:

gold coin
bone
mace
viking helmet
pelvis bone

I want to order this in the opposite way.

The end result is:
I want this to execute from the bottom up.

Code:
corpse:addItemEx(item, 1)

But I can't figure out the logical way or re-ordering the table data to do it.

EDIT: posted this in the wrong forum, derp.
 
Last edited:
Solution
Using a for loop you can easily reverse the order
Code:
local t = {
'gold coin',
'bone',
'mace',
'viking helmet',
'pelvis bone'
}
-- using the size of the table as i's base value, 1 the value to meet and then the step -1 to count backwards
for i = #t, 1, -1 do
    print(t[i])
end

-- output
pelvis bone
viking helmet
mace
bone
gold coin

So you could re-write your code like this :)
Code:
    for _, v in pairs(self:getLoot()) do
       local itemList = self:createLootItem(v, modifier)
       if itemList then
         for i = #itemList, 1, -1 do
           print(itemList[i]:getName())
           if itemList[i]:isContainer() then
             local lootContainer = self:createLootContainer(itemList[i], v, modifier)
             if...
Using a for loop you can easily reverse the order
Code:
local t = {
'gold coin',
'bone',
'mace',
'viking helmet',
'pelvis bone'
}
-- using the size of the table as i's base value, 1 the value to meet and then the step -1 to count backwards
for i = #t, 1, -1 do
    print(t[i])
end

-- output
pelvis bone
viking helmet
mace
bone
gold coin

So you could re-write your code like this :)
Code:
    for _, v in pairs(self:getLoot()) do
       local itemList = self:createLootItem(v, modifier)
       if itemList then
         for i = #itemList, 1, -1 do
           print(itemList[i]:getName())
           if itemList[i]:isContainer() then
             local lootContainer = self:createLootContainer(itemList[i], v, modifier)
             if lootContainer then
               corpse:addItemEx(itemList[i])
             end
           else
             corpse:addItemEx(itemList[i], 1)
           end
         end
       end
     end
 
Last edited:
Solution
Using a for loop you can easily reverse the order
Code:
local t = {
'gold coin',
'bone',
'mace',
'viking helmet',
'pelvis bone'
}
-- using the size of the table as i's base value, 1 the value to meet and then the step -1 to count backwards
for i = #t, 1, -1 do
    print(t[i])
end

-- output
pelvis bone
viking helmet
mace
bone
gold coin

So you could re-write your code like this :)
Code:
    for _, v in pairs(self:getLoot()) do
       local itemList = self:createLootItem(v, modifier)
       if itemList then
         for i = #itemList, 1, -1 do
           print(itemList[i]:getName())
           if itemList[i]:isContainer() then
             local lootContainer = self:createLootContainer(itemList[i], v, modifier)
             if lootContainer then
               corpse:addItemEx(itemList[i])
             end
           else
             corpse:addItemEx(itemList[i], 1)
           end
         end
       end
     end

Legend.

Code above wasn't working as it was effectively getting the table value on every loop(?), which was 1 during the loop(?).
Forgive the ignorance on my part.

But here is the resulting code I've put together that works like a dream.

Code:
    itemIndex = {}
     for _, v in pairs(self:getLoot()) do
       itemList = self:createLootItem(v, modifier)
       if itemList then
         for _, item in ipairs(itemList) do
           table.insert(itemIndex, item)
         end
       end
     end
     for i = #itemIndex, 1, -1 do
       print(itemIndex[i]:getName())
       if itemIndex[i]:isContainer() then
         local lootContainer = self:createLootContainer(itemIndex[i], v, modifier)
         if lootContainer then
           corpse:addItemEx(itemIndex[i])
         end
         else
           corpse:addItemEx(itemIndex[i], 1)
       end
     end

Gonna use this to do some more reading on the functions used.
Didn't even realize # was LUA's length operator.
 
Back
Top