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

问题描述

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

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

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

解决方法

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

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

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

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

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

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

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...