Jump to content

How to compare tables ?


Wrecking Crew

Recommended Posts

I have two tables and need to compare, for instance:

 

aircraftClientBlueCAPTypesLibrary = {'F-15C','FA-18C_hornet','F-14B','F-16C_50'}

aircraftClientBlueCAPTypes = {'F-15C','F-14B'}

 

What is the lua code that I can use in a Do Script action to return either:

  trigger.action.outText('aircraftClientBlueCAPTypes are matched', 20)

or,

  trigger.action.outText('aircraftClientBlueCAPTypes are NOT MATCHED'), 20)

?

 

TIA!

I tried a simple 'if a == b then', and a 'function do_tables_match( aircraftClientBlueCAPTypesLibrary , aircraftClientBlueCAPTypes )' but I don't know how...

 

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

I suppose it depends on if the index each entry is at matters or if it is simply checking to see if it exists. From your example I'm gonna guess its the latter. In essence "I have the table aircraftClientBlueCAPTypes and I wanna see if anything in here is in aircraftClientBlueCAPTTypesLibrary". 

 

To brute force it you can do something like this: 

 

local totMatches = 0
for i = 1, #aircraftClientBlueCAPTypes do
  for j = 1, #aircraftClientBlueCAPTypesLibrary do
     if aircraftClientBlueCAPTypes[i] == aircraftClientBlueCAPTypesLibrary[j] then
        totMatches = totMatches + 1
        break -- this breaks back to iterate the next i index.
     end
  end  
end

 

Typically for anything I do that is looking to see if a string or number exists in another table I make sure to have that item listed as a key rather than a value. For example:

 

aircraftClientBlueCAPTypesLibrary = {['F-15C'] = true, ['FA-18C_hornet'] = true, ['F-14B'] = true, ['F-16C_50'] = true}
local totMatches = 0
for i = 1, #aircraftClientBlueCAPTypes do
  if aircraftClientBlueCAPTypesLibrary[aircraftClientBlueCAPTypes[i]] then
  		totMatches = totMatches + 1   
  end 
end

 

In a practical example I'll usually have other data stored there that is useful for whatever I need. I use this table in On Station to choose which format is needed to display coordinates to different aircraft types. 

 

    local coordsUsed = {
        ['A-10C'] = {{def = 'DMS', p = 3}},
        ['AV8BNA'] = {{def = 'DMTS', p = 3}},
        ['FA-18C_hornet'] = {{def = 'DMS', p = 4}},
        ['F-14B'] = {{def = 'DMTS', p = 1}},
        ['F-16C_50'] = {{def = 'DMS', p = 3}},
    }

 

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Link to comment
Share on other sites

Thank you Grimes.  

On 2/5/2021 at 1:09 AM, Grimes said:

I have the table aircraftClientBlueCAPTypes and I wanna see if anything in here is in aircraftClientBlueCAPTTypesLibrary

I have the table aircraftClientBlueCAPTypes and I want to know if everything in here is in aircraftClientBlueCAPTypesLibrary.  The Library table can have more types in it. 

 

I will work on the code and get back with the result.

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

OK, it is all working.  This code creates two tables of Blue Client Names from the Unit field, like 'Blue A-10C 01' and 'Blue FA-18C 39'.  One table, aircraftClientBlueCASNames, holds the pilot names for CAS aircraft type roles and the other for CAP aircraft types, aircraftClientBlueCAPNames.  I have to define which aircraft go with which type roles in the CAS and CAP library tables.  So, if I add a Client aircraft to the mission and I have not put the aircraft type into the code then when the mission starts the 'missing' type(s) will be displayed.  

 

With the Blue Client names in tables, I can use those tables as conditions in Mist functions, such as:

--- code.Blue A/C In Jebel Zone
mist.flagFunc.units_in_zones{ 
    units = aircraftClientBlueCAPNames,
    zones = {'02 Jebel Zone'}, 
    flag = 90256,
    toggle = true,
}

 

 

Now, here is the code to create the tables of Blue Client names, and warn me if I forget to add a new aircraft to the definition libraries for CAP and CAS.

-- MS, code.Create Client Tables
-- aircraftClientBlueCASTypesLibrary = {'A-10A','A-10C','A-10C2','AV8BNA'} -- missing Ground Attack CAS
-- aircraftClientBlueCAPTypesLibrary = {'F-15C','FA-18C_hornet','F-14B','F-16C_50'} -- missing CAP
aircraftClientBlueCASTypesLibrary = {'A-10A','A-10C','A-10C_2','AV8BNA'} -- Ground Attack CAS
aircraftClientBlueCAPTypesLibrary = {'F-15C','FA-18C_hornet','F-14B','F-16C_50','F-14A-135-GR'} -- CAP
aircraftClientBlueNames = {}
aircraftClientBlueCASNames = {}
aircraftClientBlueCAPNames = {}
aircraftClientBlueTypes = {}
aircraftClientBlueCASTypes = {}
aircraftClientBlueCAPTypes = {}
missingClientBlueTypes = {}
aircraftClientRedNames = {}
intMatches = 0
intMatchesStart = 0
intClientTypes = 0
do
 local function contains(tbl, val)
  for _,v in ipairs(tbl) do
   if v == val then return true end
  end
  return false
 end
 for uName, uData in pairs(mist.DBs.humansByName) do
  if(contains({'red'}, uData.coalition)) then
   if not(contains(aircraftClientRedNames, uName)) then
    table.insert(aircraftClientRedNames, uName)
   end
  end
  if(contains({'blue'}, uData.coalition)) then
   if not(contains(aircraftClientBlueNames, uName)) then
    table.insert(aircraftClientBlueNames, uName)
    if not(contains(aircraftClientBlueTypes, uData.type)) then
     table.insert(aircraftClientBlueTypes, uData.type)
    end
    if(contains(aircraftClientBlueCASTypesLibrary, uData.type)) then
     table.insert(aircraftClientBlueCASNames, uName)
     if not(contains(aircraftClientBlueCASTypes, uData.type)) then
      table.insert(aircraftClientBlueCASTypes, uData.type)
     end 
    end -- Ground Attack Client
    if(contains(aircraftClientBlueCAPTypesLibrary, uData.type)) then
     table.insert(aircraftClientBlueCAPNames, uName)
     if not(contains(aircraftClientBlueCAPTypes, uData.type)) then
      table.insert(aircraftClientBlueCAPTypes, uData.type)
     end
    end -- CAP Client
   end    
  end
 end
 for i = 1, #aircraftClientBlueTypes do
  intClientTypes = intClientTypes +1
 end
 for i = 1, #aircraftClientBlueTypes do
  intMatchesStart = intMatches
  for j = 1, #aircraftClientBlueCAPTypesLibrary do
   if aircraftClientBlueTypes[i] == aircraftClientBlueCAPTypesLibrary[j] then
    intMatches = intMatches + 1
    break
   end
  end
  for k = 1, #aircraftClientBlueCASTypesLibrary do
   if aircraftClientBlueTypes[i] == aircraftClientBlueCASTypesLibrary[k] then
    intMatches = intMatches + 1
    break
   end
  end
  if intMatchesStart == intMatches then
   if not(contains(missingClientBlueTypes, aircraftClientBlueTypes[i])) then
    table.insert(missingClientBlueTypes, aircraftClientBlueTypes[i])
   end
  end
 end
end
if intClientTypes ~= intMatches then
 trigger.action.outText('There are missing Blue Client Types in the Libraries.', 20)
 trigger.action.outText('Missing Blue Client Types', 20)
 table.sort(missingClientBlueTypes)
 for index, data in pairs(missingClientBlueTypes) do
  trigger.action.outText(tostring(data), 20, false)
 end
 trigger.action.outText('\n aircraftClientBlueCASTypesLibrary', 20)
 table.sort(aircraftClientBlueCASTypesLibrary)
 for index, data in pairs(aircraftClientBlueCASTypesLibrary) do
  trigger.action.outText(tostring(data), 20, false)
 end
 trigger.action.outText('\n aircraftClientBlueCAPTypesLibrary', 20)
 table.sort(aircraftClientBlueCAPTypesLibrary)
 for index, data in pairs(aircraftClientBlueCAPTypesLibrary) do
  trigger.action.outText(tostring(data), 20, false)
 end
end

 

 

Test ClientNamesByType 256S1e.miz

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...