cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
lala
Level VIII

怎样用脚本计算不同字符串中字符相同的个数。

怎样用脚本实现将数据表的内容与第二个表中内容匹配的字符数量,不考虑顺序 、相符合数据最多的返回第二个表的行号。

 

我只会编写简单的循环比较。

如果要比较的字符串数量很多时、怎样才能较快速。

谢谢!

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: 怎样用脚本计算不同字符串中字符相同的个数。

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;
);

jthi_0-1652283682820.png

 

-Jarmo

View solution in original post

6 REPLIES 6
lala
Level VIII

Re: 怎样用脚本计算不同字符串中字符相同的个数。

For example,

string for the first table

"KDBAC"
"FLGHEK"
"ONLCQR"
"WVTZXR"

 

String for the second table

"ROMNLQ"
"DFBACE"
"LKGHJK"
"WVTZYX"
lala
Level VIII

Re: 怎样用脚本计算不同字符串中字符相同的个数。

2022-05-11_21-28-12.png

jthi
Super User

Re: 怎样用脚本计算不同字符串中字符相同的个数。

Can there be duplicated characters in the strings?

-Jarmo
jthi
Super User

Re: 怎样用脚本计算不同字符串中字符相同的个数。

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;
);

jthi_0-1652283682820.png

 

-Jarmo
lala
Level VIII

Re: 怎样用脚本计算不同字符串中字符相同的个数。

JMP Pro 15.2,

Thank jthi!

2022-05-12_08-54-42.png

jthi
Super User

Re: 怎样用脚本计算不同字符串中字符相同的个数。

If you are using JMP15 you have to change For Each to For-loop.

-Jarmo