Comparing Tables

In Hollywood you can compare tables using something like this:

Local table1 = { a = 10 }
Local table2 = { a = 10 }

If table1 = table2 Then DebugPrint("table1 and table2 are equal!")

However the above comparison will return always FALSE because the comparison is made using the table pointers and not their contents.

If you add the following code to the previous example you will understand what I mean:

DebugPrint(table1, table2)

As you can see the two tables point to two different memory chunks, the standard comparison will work only on two variables pointing to the same table like this:

Local table1 = { a = 10 }
Local table2 = table1
If table1 = table2 Then DebugPrint("table1 and table2 are equal!")

That's the reason why I have developed some functions to compare two tables by their contents.

TB.TEST_Compare()

The function TB.TEST_Compare() performs a test and shows how you can use the comparing functions :

  • TB.Compare(table1, table2, compare_funcs)
  • TB.CompareScore(table1, table2, compare_funcs, count, equals)

Here is the example. First of all we will define two tables composed by subtables (records) with a single small difference between them:

  Local tab01 = { { name = "Spiderman", power = "Web, Speed, Strenght" },
                  { name = "Thor", power = "Hammer" },
                  { name = "Flash", power = "Speed" },
                  { name = "Iron Man", power = "Armour" },
                  { name = "Iron Man", power = "Weapons" },
                  { name = "Hulk", power = "Strenght" } }

  Local tab02 = { { name = "Spiderman", power = "Web" },  ; <== Here is the differce from the previous table
                  { name = "Thor", power = "Hammer" },
                  { name = "Flash", power = "Speed" },
                  { name = "Iron Man", power = "Armour" },
                  { name = "Iron Man", power = "Weapons" },
                  { name = "Hulk", power = "Strenght" } }

Then we will print to the screen the contents of the two tables with a simple loop:

  NPrint("TABLE 01 CONTENT")
  For i = 0 To 5
    Local v = tab01[i]
    NPrint("INDEX : [color=#red]" .. i .. "[/color], NAME : [color=#yellow]" .. v.name .. "[/color], POWER : " .. v.power)
  Next
      
  NPrint("\nTABLE 02 CONTENT")
  For i = 0 To 5
    Local v = tab02[i]
    NPrint("INDEX : [color=#red]" .. i .. "[/color], NAME : [color=#yellow]" .. v.name .. "[/color], POWER : " .. v.power)
  Next
  NPrint("-> Please note that the two tables have a different value in item 0")

And now we will use the comparison functions:

  NPrint("\n[b]COMPARING USING TB.Compare()...[/b]")
  Local result = TB.Compare(tab01, tab02, False)
  
  NPrint(IIf(result, "THE TABLES HAVE THE SAME CONTENTS", "THE TABLES DOES NOT HAVE THE SAME CONTENTS"))

We can also calculate a % score that indicate how much the two tables are equal:

  NPrint("\n[b]COMPARING USING TB.CompareScore()...[/b]")
  Local score, compared, equals = TB.CompareScore(tab01, tab02, False)
  
  NPrint("SCORE          : " .. ToString(Int(Score*10000)/100) .. "%")
  NPrint("COMPARED ITEMS : " .. ToString(compared))
  NPrint("EQUAL ITEMS    : " .. ToString(equals))
  
  NPrint("\nLeft mouse to QUIT.")
  
  WaitLeftMouse()
hollywoodlibs/tables/testcompare.txt · Ultima modifica: 2016/10/16 11:55 da admin
CC Attribution-Noncommercial 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0