array how to delete rows with value equals to zero

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #246942 quote
    oliTR
    Participant
    Junior

    Dear all,

    in an array, what would be the code to delete rows that have value equals to zero?

    #246947 quote
    JC_Bywan
    Moderator
    Master

    It depends on what your purpose is…

    For example if you want a smaller array with non-zero values only, you can transfer an array into another array by looping until max rank of starting array and selecting only the non-zero values to be transferred into destination array, but you would lose the initial ranks, so the purpose is important to start with in order to decide whether losing ranks is compatible with the rest of your code.

    Another possibility is: if you want to keep same array and same ranks and what you’re after is the smallest non-zero value, then there would be several different coding possibilities, still depending on final purpose.

    But if what you’re after is deleting all intermediate zero values in ranks lower than the max rank, while keeping existing ranks of non-zero values where they are, meaning switching back some zeros to “undefined”, regardless of why, this 3rd possibility couldn’t be done.

    robertogozzi, druby, oliTR and Iván González thanked this post
    #246956 quote
    druby
    Participant
    New

    One technique I have used is, if the values stored in the array are all going to be positive and/or zero, then multiplying the non required value(s)  by -1 makes it/them negative. This allow using a condition that skips the negative values when looping through the array. Depending on application, the CONTINUE and BREAK commands can be useful  with the IF statement.. The array stays the same size and the original values are all still intact, though some may be set negative, but multiply by -1 again on a value  revert the back to original representation. Index order still maintained with array. Multiplying the whole array inverts the valid elements, to give the invalid values if needed.

    oliTR and Iván González thanked this post
    #246962 quote
    druby
    Participant
    New

    When you state, ‘delete rows’, I assume that you mean removing the un-wanted element values and then only being left with the minimum number of elements in the array, or in one continuous block of elements.

    Unfortunately, there is no instruction that deletes a single array element, only way to delete an element is to use UNSET($arr), which un-set’s the whole array.

    To achieve the goal, you would have to copy all required elements to a temporarily array, only incrementing the new index for required values. The temp array would then hold the wanted values in the smallest array size.

    However, you would have to UNSET the original array and re-create it again and copy the temp array to the new array with original name. This is so that any reference to the original array name referenced in the code logic would still work. There is no re-naming of arrays. Then the temp could be UNSET, for next time. LASTSET points to the highest index which is a valid element value.

    One thing to note is that, LASTSET($arr) all-way points to the highest valid defined array index, if I remember correctly.

    If you just shifted the next required elements value into the un- required values element, continue shifting till all the valid values were in one block, the excess  end element(s) will still hold there original values from before the shifting.  These excess elements could be set to UNDEFINED or some control value, but the elements would still exist, the last pointed to element index   by LASTSET($arr) would be the last valid defined array index of the array, which would not be a valid element value. You could use a separate variable to track the end of the required array elements end position.

    oliTR thanked this post
    #246963 quote
    druby
    Participant
    New

    Further to the shifting scenario, you could start at the end of the array and shift the element up. The excess elements would fall at the beginning of the array and the external variable could track the start point of valid values. This would mean LASTSET would both point to the last array element which would be valid, so LASTSET could still be easily used. Just the start point would move.

    oliTR thanked this post
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.

array how to delete rows with value equals to zero


ProOrder: Automated Strategies & Backtesting

New Reply
Author
author-avatar
oliTR @olitr Participant
Summary

This topic contains 4 replies,
has 3 voices, and was last updated by druby
10 months, 1 week ago.

Topic Details
Forum: ProOrder: Automated Strategies & Backtesting
Language: English
Started: 05/10/2025
Status: Active
Attachments: No files
Logo Logo
Loading...