Jump to content

ArrayPush UDF...


hgeras
 Share

Recommended Posts

The functionality is as that: You tell it to to store a new value at the first or the last element and delete the last or first one , depending the direction you chose...So you will have new values and the others will "slide" left or right... You can see a need for application of this function in my NvTempLogger script (check sig) , the Array_Slide_left function...Of course this UDF is more improved since you can choose direction and the new value can also be another array...Consider it the opposite of _ArrayPop UDF....

;=====================================================================================
;
; Function Name:    _ArrayPush
; Description:    Add new values without increasing array size.Either by inserting
;                  at the end the new value and deleting the first one or vice versa.
; Parameter(s):  $avArray     - Array
;                  $sValue     - The new value.It can be an array too.
;                  $i_Direction  - 0 = Leftwise slide (adding at the end) (default)
;                                  1 = Rightwise slide (adding at the start)         
; Requirement(s):   None
; Return Value(s):  On Success -  Returns 1
;                  On Failure -  0 if $avArray is not an array.
;                                -1 if $sValue array size is greater than $avArray size.
;                                In both cases @error is set to 1.
; Author(s):        Helias Gerassimou(hgeras)
;
;======================================================================================



Func _ArrayPush(ByRef $avArray, $sValue, $i_Direction = 0)
    Local $i,$j
    
    If (Not IsArray($avArray)) Then
        SetError(1)
        Return 0
    EndIf
    

If (Not IsArray($sValue)) Then  
    If $i_Direction = 1 Then
        For $i = (UBound($avArray) - 1) To 1
            $avArray[$i] = $avArray[$i - 1]
        Next
        $avArray[0] = $sValue
        
    Else
        
        For $i = 0 To (UBound($avArray) - 2)
            $avArray[$i] = $avArray[$i + 1]
        Next
        Local $i = (UBound($avArray) - 1)
        $avArray[$i] = $sValue
        
    EndIf
    
    SetError(0)
    Return 1
Else
    If UBound($sValue) > UBound($avArray) Then
        Seterror(1)
        Return -1
    Else
    
    For $j = 0 to (UBound($sValue) - 1)
        If $i_Direction = 1 Then
            For $i = (UBound($avArray) - 1) To 1
                $avArray[$i] = $avArray[$i - 1]
            Next
            $avArray[$j] = $sValue[$j]
        
        Else
        
            For $i = 0 To (UBound($avArray) - 2)
                $avArray[$i] = $avArray[$i + 1]
            Next
            Local $i = (UBound($avArray) - 1)
            $avArray[$i] = $sValue[$j]
        
        EndIf
    Next
    EndIf
EndIf
    
    SetError(0)
    Return 1
    
    
EndFunc  ;==>_ArrayPush

I hope you make a good use of it....

C ya

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...