它们是一次比较多个字符串并找出重复项的更快方法吗?
我有5个课程代码供用户输入。如果用户输入重复的课程代码,则错误消息应显示“检测到重复的代码”。
IF((WS-STUCODEL1 > 1) AND (STUCODE1 = STUCODE2
OR STUCODE3 OR STUCODE4 OR STUCODE5))
MOVE 'DUPLICATE CODES DETECTED' TO WS-MSG
MOVE 'B' TO WS-CURSOR
GO TO 400-RETURN
ELSE IF
答案 0 :(得分:3)
假设有很多课程代码,然后
SET NO-DUPLICATE-FOUND TO TRUE
IF STUCODE5 NOT = SPACES
IF STUCODE5 = STUCODE1 OR STUCODE2 OR STUCODE3
OR STUCODE4
SET DUPLICATE-FOUND TO TRUE
END-IF
END-IF
IF STUCODE4 NOT = SPACES
IF STUCODE4 = STUCODE1 OR STUCODE2 OR STUCODE3
SET DUPLICATE-FOUND TO TRUE
END-IF
END-IF
IF STUCODE3 NOT = SPACES
IF STUCODE3 = STUCODE1 OR STUCODE2
SET DUPLICATE-FOUND TO TRUE
END-IF
END-IF
IF STUCODE2 NOT = SPACES
IF STUCODE2 = STUCODE1
SET DUPLICATE-FOUND TO TRUE
END-IF
END-IF
IF DUPLICATE-FOUND
MOVE 'DUPLICATE CODES DETECTED' TO WS-MSG
MOVE 'B' TO WS-CURSOR
END-IF
GO TO 400-RETURN
可能相当快。
请注意,如果任何课程代码为空白,则将跳过与该课程代码的其余比较。这意味着任何SPACES
中的STUCODEn
都不会造成重复。
答案 1 :(得分:3)
如果要检查的条目数量很大或可能会更改,则可以使用表来保存键入内容是合理的...
一种选择是使用两个PERFORM(test code online):
SET NO-DUPLICATE-FOUND TO TRUE
MOVE 1 TO IND
PERFORM VARYING IND FROM 1 BY 1
UNTIL IND = NUMBER-OF-STUCODES
MOVE STUCODE (IND) TO COMPCODE
IF COMPCODE NOT = SPACES
ADD 1 TO IND GIVING IND-2
PERFORM UNTIL IND-2 > NUMBER-OF-STUCODES
IF COMPCODE = STUCODE (IND-2)
SET DUPLICATE-FOUND TO TRUE
EXIT PERFORM
END-IF
ADD 1 TO IND-2
END-PERFORM
IF DUPLICATE-FOUND
MOVE 'DUPLICATE CODES DETECTED' TO WS-MSG
MOVE 'B' TO WS-CURSOR
EXIT PERFORM
END-IF
END-IF
END-PERFORM
使用一个PERFORM和一个SEARCH(test code online)索引表的另一种选择:
SET NO-DUPLICATE-FOUND TO TRUE
PERFORM VARYING EXTRA-IND FROM 1 BY 1 *> not all compilers support VARYING on USAGE INDEX
UNTIL EXTRA-IND = NUMBER-OF-STUCODES
MOVE STUCODE (EXTRA-IND) TO COMPCODE
IF COMPCODE NOT = SPACES
SET IND TO EXTRA-IND
SET IND UP BY 1
SEARCH STUCODE
WHEN STUCODE(IND) = COMPCODE
SET DUPLICATE-FOUND TO TRUE
END-SEARCH
IF DUPLICATE-FOUND
MOVE 'DUPLICATE CODES DETECTED' TO WS-MSG
MOVE 'B' TO WS-CURSOR
EXIT PERFORM
END-IF
END-IF
END-PERFORM