Forum Discussion

tvklovesu's avatar
tvklovesu
Frequent Contributor
11 months ago

how to compare data in two table variables

Hello,

I have created two variables of table type and add data in both the tables. I want to check if the data from table 1 in table 2 and not sure how to get that done. Below is the snap shot of my script

 

Table one

var tab = ProjectSuite.Variables.VariableByName("htsCodes")

resCounts = 3

for (i = 0; i <resCounts; i++){
var j = i+1
var codeValue = Aliases.Page.discrpcy_results_Table.FindElement("./tbody/tr[" + j + "]/td["+columnValue+"]").textContent
tab.$set("htsCodes", i, codeValue)
}

 

Table Two

var tab = ProjectSuite.Variables.VariableByName("htsCodesinDescription")

resCounts = 5

for (i = 0; i <resCounts; i++){
var j = i+1
var codeValue = Aliases.Page.results_Table.FindElement("./tbody/tr[" + j + "]/td["+columnValue+"]").textContent
tab.$set("htsCodesinDescription", i, codeValue)
}

 

now I want to check if Table two[i] value exists in Table one

Something like

table1=["11","12", "13"]

table2 = ["11,"12", "13","14","15"]

Compare

var table2 = ProjectSuite.Variables.VariableByName("htsCodesinDescription")

var table1 = ProjectSuite.Variables.VariableByName("htsCodes")

for (i=0; i<=table2.RowCount; i++){

    value = tab.$get("htsCodesinDescription", i)

    if (table1.contains(value)){    //for me it says table1.contains is not a function

       pass

    }

    else{

       fail

    }

}

4 Replies

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I've given you a structure which you should be able to work from. You will need to adjust the logic to peform what you want. For example, if a match is found then don't continue with the remaining iteration. Else, if a match is not found, then print the item that's not found.

     

    There are plenty of examples on the internet, but the best method is trying to resolve the issue yourself. If you get struck, then post your code and I'll help.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Here's an example,

    function TableTest()
    {
        var table1 = ["a", "b", "c", "d", "e"];
        var table2 = ["a", "b", "D", "f"];
        
        for (var i = 0; i < table1.length; i++) {
            for (var j = 0; j < table2.length; j++) {
                if (aqString.Compare(table1[i], table2[j], true) == 0) {   
                    Log.Message("Matched");
                } else {
                    Log.Warning("No Match");
                }
            }
        }
    }

    I get the first item from table1, and compare it with each of the items in table2. I then get the second item from table1, and compare it with each of the items in table2. Etc.

     

    The matched items will be "a" and "b".

    • tvklovesu's avatar
      tvklovesu
      Frequent Contributor

      rraghvani, Thanks for your example. I thought of doing the same but my issue is that at the end of the list comparision I want to know what all the values doesn't exists in table2. In your example on iteration one "a" matches, but "b" not matched and on iteration two "a" doesn't match but "b" matched. So that will not help me to make sure that only "a" and "b" matched but "c", "d", "e", "D", "f" doesn't match in both tables.

      • tvklovesu's avatar
        tvklovesu
        Frequent Contributor

        I was able to resolve the issue by using Array.indexOf. That helped me to find if the table 2 contains the row value[i] from table 1.