如何检测并删除 RPGLE 数组中的重复数据?

How to detect and then remove duplicate data in an RPGLE array?

我正在基于业务逻辑在 RPGLE 程序中加载数组,这可能会导致数组中出现重复数据。

我首先想知道如何检测重复。

最后我想知道如何删除数组中的重复项。

检测数组中的重复条目非常简单:

0.) sort the array 
1.) loop through the array and check if the current item matches the previous item 
2.) if so, then remove the current item or 

或者你可以

0.) create a temporary array
1.) loop through the original array and check if the current item is already contained in the temp arrayitem 
2.) if not, then copy the current item into the temp array
3.) clear the original array and copy them from the temp to the oiginal array

这里你有一个关于一般主题的 SO 问题:Array remove duplicate elements 这是关于在 RPGLE 中执行此操作的线程:https://code400.com/forum/forum/iseries-programming-languages/rpg-rpgle/7270-check-for-duplicates-in-array-of-size-50

如果删除重复项后原始数组中的条目顺序必须相同,则第二种方法会更好

编辑:犯了一个错误,第二种方法不需要任何排序(删除了这一点)

您可以在添加之前使用 %LOOKUP 查看该条目是否已在数组中。

if %lookup(newValue : array : 1 : numElems) = 0;
   // the element is not in the array yet
   numElems += 1;
   array(numElems) = newValue;
endif;