How to use a script to achieve the number of characters that match the content of the data table with the content of the second table, regardless of the order, and return the row number of the second table with the most matching data.
I will only write simple loop comparisons.
How can it be faster if the number of strings to be compared is large.
Thanks!
For example,
string for the first table
"KDBAC"
"FLGHEK"
"ONLCQR"
"WVTZXR"
String for the second table
"ROMNLQ"
"DFBACE"
"LKGHJK"
"WVTZYX"
Can there be duplicated characters in the strings?
If no duplication is allowed you could try using associative arrays and their set operations:
Names Default To Here(1);
dt = New Table("demo",
Add Rows(4),
New Column("A", Character, "Nominal", Set Values({"KDBAC", "FLGHEK", "ONLCQR", "WVTZXR"})),
New Column("B", Character, "Nominal", Set Values({"ROMNLQ", "DFBACE", "LKGHJK", "WVTZYX"}))
);
// Create lookup associative array, with row number as key and value as associative array with
// keys as characters in row. This will be used in union operations
aa_lookup = Associative Array();
For Each({val, idx}, dt:B << get values,
aa_lookup[idx] = Associative Array(Eval List(Words(val, "")));
);
// Create collection column
dt << New Column("C", Numeric, Continuous);
For Each Row(dt,
best_match = 0;
max_row = .;
For Each({{key, val}}, aa_lookup,
cur_aa = Associative Array(Eval List(Words(:A, "")));
cur_aa << Intersect(val);
cur_match = N Items(cur_aa);
If(cur_match > best_match,
best_match = cur_match;
max_row = key;
);
);
:C = max_row;
);
