如何通过使用ENTRY函数获取来自两个不同句子的常见单词?
如何使用ENTRY
function in progress4gl从两个不同的句子中获取常用单词?如何通过使用ENTRY函数获取来自两个不同句子的常见单词?
define variable a1 as character no-undo initial "hi dude do".
define variable a2 as character no-undo initial "hi man it".
define variable cnta as character.
define variable cntb as character.
define variable cntc as character.
define variable i as integer.
define variable j as integer.
do i = 1 to 3:
entry (i,a1,"").
do j = 1 to 3:
entry (j,a2,"").
end.
end.
/* assign cntc = cnta matches cntb . */
define variable a1 as character no-undo initial "hi dude do".
define variable a2 as character no-undo initial "hi man it".
define variable common as character no-undo.
define variable cc as integer no-undo.
define variable ii as integer no-undo.
define variable jj as integer no-undo.
define variable n1 as integer no-undo.
define variable n2 as integer no-undo.
n1 = num-entries(a1).
n2 = num-entries(a2).
do ii = 1 to n1:
do jj = 1 to n2:
if entry (ii, a1, " ") = entry(jj, a2, " ") then
do:
cc = cc + 1.
common = common + " " + entry(ii, a1, " ").
end.
end.
end.
display trim(cc) common.
注:
的TRIM()函数只是清理“共同”的字符串,因此不会有多余的空间。
出于性能方面的考虑,习惯于在循环之外获取NUM-ENTRIES()而不是循环的每次迭代。它对小字符串没有太大的区别,但是对于大字符串它可以产生相当大的影响。
条目和num-entries的默认分隔符不是空格,而是逗号。我也会把修剪放在循环之外。 –
Arg!你是对的。我在想进口&出口 –
看着斯里的两个职位,我可能还会注意到“”“”。如果意图是应该将空格字符当作单词之间的分隔符,那么quote-space-quote是需要为分隔符参数指定的内容。不是报价。 –
1./*如果我需要在运行时从用户那里得到两个句子的n个单词,如何比较和获取常用单词。
下面的代码只比较和显示两个句子的第一个字母。 */
define variable a1 as character no-undo.
define variable a2 as character no-undo.
define variable common as character no-undo.
define variable a1 as character FORMAT "x(64)" no-undo /* initial "hi d do" */.
define variable a2 as character FORMAT "x(64)" no-undo /* initial "hi d it" */.
define variable common as character FORMAT "x(64)" no-undo.
define variable c1 as character FORMAT "x(64)" no-undo.
define variable x as character FORMAT "x(64)" no-undo.
define variable y as character FORMAT "x(64)" no-undo.
define variable cc as integer no-undo initial 0.
define variable ii as integer no-undo.
define variable jj as integer no-undo.
define variable n1 as integer no-undo.
define variable n2 as integer no-undo.
set a1.
n1 = num-entries(a1,"").
set a2.
n2 = num-entries(a2,"").
do ii = 1 to n1:
do jj = 1 to n2:
if entry (ii,a1, " ") matches entry(jj,a2, " ") then
do:
common = entry(ii, a1, " ").
display common .
end.
end.
end.
你在问新的问题吗? –
使用MATCHES来测试简单的相等性是一个坏主意。如果你形成这样的习惯,然后在WHERE子句中使用它,你将会阻止索引包围并导致大量的表扫描。 –
1.您提供输入数据,但不提供预期的输出数据。预期的输出是“hi”? 2.为什么问题试图限制应该用来解决问题的函数(ENTRY)?这听起来像是面试问题。 3.什么是cnta,cntb和cntc试图说明? –
如果我们要回答面试问题,我们会获得签名奖金吗? –