Item Comparisons

Tables library comes with a very useful function to compare records, with records I mean a tables with the same members.

A possible scenario could be that you have a table that works as a database, each entry is a subtable (record) that have exactly the same structure (fields).

With this function you can easily compare them specifying the comparison type and the fields you want to compare, here is the example coming with the library.

Function TB.TEST_ItemCompare()
  ; Compares one or more fields of two records, if the comparison is
  ; between two equal values then the next specified fields will be
  ; checked otherwise the result of the first comparison is returned.
  DebugPrint("TESTING TB.Item.Compare()")
  
  Local record1 = { name = "Spiderman", age = 34, job = "Super Hero" }
  Local record2 = { name = "Batman",    age = 41, job = "Super Hero" }
  
  DebugPrint("Comparing field 'name' : record1.name > record2.name ?")
  DebugPrint(record1.name .. " > " .. record2.name .. " ?")
  Local result = TB.Item.Compare(record1, record2, { True }, { "name" })
  DebugPrint(IIf(result, "YES", "NO"))
  DebugPrint("")
  
  DebugPrint("Comparing field 'age' : record1.age < record2.age ?")
  DebugPrint(record1.age .. " < " .. record2.age .. " ?")
  Local result = TB.Item.Compare(record1, record2, { False }, { "age" })
  DebugPrint(IIf(result, "YES", "NO"))
  DebugPrint("")
  
  DebugPrint("Comparing fields 'job', 'age' : record1.job < record2.job, record1.age < record2.age ?")
  DebugPrint(record1.job .. " < " .. record2.job .. ", " .. record1.age .. " < " .. record2.age .. " ?")
  Local result = TB.Item.Compare(record1, record2, { False, False }, { "job", "age" })
  DebugPrint(IIf(result, "YES", "NO"))
  DebugPrint("\nNOTE:\nThe result is YES because the first comparison is between\ntwo equal values so the check continue to the next specified field.")
  
  
  DebugPrompt("Hit ENTER to quit?")
EndFunction
hollywoodlibs/tables/testitemcompare.txt · Ultima modifica: 2016/10/21 00:20 da admin
CC Attribution-Noncommercial 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0