【问题标题】:How to detect and then remove duplicate data in an RPGLE array?如何检测然后删除 RPGLE 数组中的重复数据?
【发布时间】:2021-02-08 08:19:58
【问题描述】:

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

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

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

【问题讨论】:

    标签: ibm-midrange rpgle


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      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

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

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

      【讨论】:

      • 对于您的第一种方法,您建议如何执行第 3 步(“删除当前项目”)?我可以想到几种方法来做到这一点,但它们都非常复杂和/或性能不佳。
      • 我只会“删除”这些项目(将它们设置为 NULL 值或其他内容)。然后,您将不得不取出所有剩余的项目并将它们移动 1 个索引(这使得算法的复杂性非常高),或者您将它们“归零”,并在清理原始数组后转移“非归零” " 值到另一个数组,清除原始数组并将它们传回。所以我认为你不能绕过第二个数组
      • 但是在考虑到这一点之后,我肯定可以在这种情况下推荐方法#2。就像下面/上面显示的 Barbara Morris 代码一样。
      • Radinator,您关于清除已删除项目的想法可能会奏效。如果将不需要的元素设置为 *HIVAL,则可以在循环后再次对数组进行排序,然后将“元素数量”变量减去“已删除”元素的数量。
      • 是的,这是方法 :D 但正如我在帖子中所说,如果元素的顺序必须与“输入”相同,则应该遍历元素并将它们复制到临时数组仅当它们尚未包含在临时数组中时
      猜你喜欢
      • 2021-05-12
      • 1970-01-01
      • 1970-01-01
      • 2016-09-04
      • 2022-01-19
      • 1970-01-01
      • 2011-07-10
      • 2019-04-23
      • 2021-06-17
      相关资源
      最近更新 更多