Old 12-31-2018, 10:09 PM   #1
Shreebz
Human being with feelings
 
Join Date: Apr 2018
Posts: 21
Default Compare two tables and save differences

Happy new year to you all!

I have a script that imports audio files from a text file whitelist. After they're imported I also make a table of what's in the session. It's possible that the whitelist isn't 100% copied, and I'd like to save what isn't imported to the session to its own text file. What's the best way to go about finding differences between two tables? I'm pretty confused about how to compare two tables, and save the differences between them.
Shreebz is offline   Reply With Quote
Old 01-01-2019, 05:47 AM   #2
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 5,324
Default

It depends on what you consider difference (index per index comparison, or even key differnence, or just the "has this table a value or not) and what king of report you need (just indexes, indexes + value etc)
The key is to try to NOT compare all the entry from a table to all the entry of the other cuase this doesn't scale well and can be greedy on CPU exponentially.


So the basic trick would be to create a new table and use keys based on values on the two tables.
If one table has the value assign to this key, then increment the table.key value. IN the end, you will have a table of keys and their number of occurence between the two table (one or two), with just one loop per table. Not sure if it is clearly explained but at least it should give you some hint to follow ^^



But again, this depends on what you need exactly as report.
X-Raym is offline   Reply With Quote
Old 01-01-2019, 06:03 AM   #3
snooks
Human being with feelings
 
Join Date: Sep 2015
Posts: 1,578
Default

Here's a generic thing that gets a table of differences. You can see how it works so you can, as X-Raym says, modify it for your own purpose if you don't need all differences:
Code:
local t1 = { "array1", "array2",  bling = "blah", blop = "meh",          blurp = "bof" }
local t2 = { "array1", "array21", bling = "blah", --[[ blop missing --]] blurp = "different", xtra = "not in t1"}

local function printTable(t)
  for k, v in pairs(t) do
    reaper.ShowConsoleMsg(k .. "\t" .. v .. "\n")
  end
end

local function getDiffTables(t1, t2)
  local diff = {}
  -- get changed or missing entries in t2 compared to t1
  for k, v in pairs(t1) do
    if t2[k] ~= v then
      if t2[k] == nil then
        diff[k] = "-- entry not in second table --"
      else
        diff[k] = t2[k]
      end
    end
  end

  -- get additional entries in t2 that are not in t1
  for k, v in pairs(t2) do
    if t1[k] == nil then
      diff[k] = v
    end
  end
  return diff
end

printTable(getDiffTables(t1, t2))
snooks is offline   Reply With Quote
Old 01-02-2019, 01:22 PM   #4
mespotine
Human being with feelings
 
mespotine's Avatar
 
Join Date: May 2017
Location: Leipzig, Germany
Posts: 679
Default

You can also use my Ultraschall-API(link to the thread in my signature), which has this nice function:

https://mespotin.uber.space/Mespotin...atesFromArrays

It returns duplicates from two given tables as well as the entries that are only in each of one of the tables.
This might do the job.
__________________
Ultraschall-API: https://forum.cockos.com/showthread....98#post2067798
Reaper Internals - Developerdocs for Reaper: https://forum.cockos.com/showthread.php?t=207635
mespotine is offline   Reply With Quote
Old 01-05-2019, 12:43 PM   #5
Shreebz
Human being with feelings
 
Join Date: Apr 2018
Posts: 21
Default

Thanks for the input, everyone.

I wound up not implementing the diffing solution because it's a lot more difficult than I thought to get the files which failed to copy. Despite the file not existing, Reaper imports and inserts it anyway as a blank item. It's a properly named item, it just has no audio file associated with it. So, my array that gets everything in the session, after import, is filled with the entire whitelist regardless of whether or not a file is successfully copied.

Fortunately, the project bay shows the status of a "failed" file as "Unavailable". So, I can always use that list as my guide.

Last edited by Shreebz; 01-05-2019 at 03:01 PM.
Shreebz is offline   Reply With Quote
Old 01-05-2019, 01:59 PM   #6
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 5,324
Default

why not using reaper.file_exists function ?
X-Raym is offline   Reply With Quote
Old 01-05-2019, 02:57 PM   #7
Shreebz
Human being with feelings
 
Join Date: Apr 2018
Posts: 21
Default

Quote:
Originally Posted by X-Raym View Post
why not using reaper.file_exists function ?
Heh, well, because I didn't know that function existed. D'oh!

Thanks for the tip!
Shreebz is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 07:38 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.