Help with an algorithm to count items in a table

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Help with an algorithm to count items in a table

Post by nukkxx5058 »

Hi,
I need to count how many of each type of cargo (dbid) there is in a unit. But can't figure out the right algo.
So far I can obtain this result, a loop returning in the console the list of all cargo guid's loaded in the unit.
Output is:
2346
2346
2568
2568
2568
2436
2436
2266
2266
2266
So I can see that in this example, in the unit, there are:
2 * #2346
3 * #2568
2 * #2436
3 * #2266

Of course the size of the file is unknown.

But can't find the right algorithm to have the program do it for me ... Yes, I know it's basic :mrgreen:
But would appreciate some help ...
-------------------------------------------------------
local selectedUnit = ScenEdit_SelectedUnits()
local unitGuid = selectedUnit.units[1].guid
local c = ScenEdit_GetUnit({guid=unitGuid}).components
local i = 0
local cargoType

for _ in pairs(c) do
if c[_].comp_type == 'Cargo' then
cargoType = c[_].comp_dbid
i = i + 1
print(cargoType)
end
end
Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Help with an algorithm to count items in a table

Post by KnightHawk75 »

Code: Select all

-- returns table of dbid counts or empty table.
local function countCompDBIDs(srcTable)
  if (srcTable ==nil) or type(srcTable) ~= "table" then print("Missing or nil table parameter."); return {}; end
  local ct = {}; --hold the running counts. 
  for i =1, #srcTable do
    if ct[srcTable[i].comp_dbid] ~= nil then 
      ct[srcTable[i].comp_dbid] = ct[srcTable[i].comp_dbid] + 1; --increment from last.
    else
      ct[srcTable[i].comp_dbid] = 1; --first iteration.
    end
  end
  return ct;
end

local sus = ScenEdit_SelectedUnits();
local rettbl;
if (sus.units ~= nil) and #sus.units > 0 then
  rettbl = countCompDBIDs(ScenEdit_GetUnit({guid=sus.units[1].guid}):filterOnComponent('Cargo'));
  print(rettbl);
else
  print("No units selected.");
end

-- sample output of [dbid]=count table --
-- { [616] = 24, [3273] = 32, [1963] = 1, [1044] = 2, [3147] = 4, [1974] = 1, [1043] = 1 }
-- ie there are 24 of type 615's, 32 of type 3273's etc.
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Re: Help with an algorithm to count items in a table

Post by nukkxx5058 »

That's cool ! And works perfect. Thank you ! :-)
Now will have to understand it :-)
Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Re: Help with an algorithm to count items in a table

Post by nukkxx5058 »

There's just one small issue: when 2 hangars are selected. I will fix this by adding a warning " error: you selected more than one unit".
Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Help with an algorithm to count items in a table

Post by KnightHawk75 »

nukkxx5058 wrote: Thu Mar 10, 2022 6:13 pm There's just one small issue: when 2 hangars are selected. I will fix this by adding a warning " error: you selected more than one unit".
Do you want it to count all selected units (and not just the first), and keep a total per unit, and/or total per selection? I can change it to do that.
User avatar
nukkxx5058
Posts: 3141
Joined: Thu Feb 03, 2005 2:57 pm
Location: France

Re: Help with an algorithm to count items in a table

Post by nukkxx5058 »

Not 100% sure yet. My idea was to get the total per dbid for one base. So for x units. I think the player will have to select either an individual hangar or a group of hangars /tarmacs spaces etc. and unload the cargo. So it should work for both. So yes, multiple units is fine and a total per cargo dbid.
But I think I should be able to find the way :-) My main problem was the counting algorithm :)
Thank you
Winner of the first edition of the Command: Modern Operations COMPLEX PBEM Tournament (IKE) (April 2022) :-)
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Help with an algorithm to count items in a table

Post by KnightHawk75 »

Image
Post Reply

Return to “Lua Legion”