;; 
;;=====================================================================================----- 
;; 
;;FUNCTION       Edit() 
;; 
;;ACTION         Performs a line edit on data, either newline-delimited string or array 
;; 
;;AUTHOR         Glenn Barnas 
;; 
;;VERSION        1.0 - 2020/08/09 
;; 
;;HISTORY        1.0 - 2020/08/09 - Initial Release 
;; 
;;SYNTAX         Edit(Data, Action [, Line] [, D1] [, D2]) 
;; 
;;PARAMETERS     Data   - REQUIRED - The data to operate on. Can be an array or a string 
;;                                   delimited with CRLFs or Newlines. 
;;               Action - REQUIRED - The action to perform - see below: 
;;                                      0 = Replace Line with D1 
;;                                      1 = Insert D1 at D2 
;;                                          D2 = 'text' will insert D1 where D2 is first  
;;                                               found, or at the beginning of the line 
;;                                               if D2 is not found. 
;;                                          D2 = '#<digits>' will insert D1 at the specified 
;;                                               character position, or will append if the 
;;                                               position is > the line text length. 
;;                                      2 = Append D1 after D2 text (first occurrence) 
;;                                      3 = Replace D1 with D2 (all occurrences) 
;;                                      4 = Remove D1 from line (all occurrences) 
;;                                      5 = Insert D1 at Line, moving all lines down 
;;                                      6 = Delete Line, moving all lines up 
;;                                      9 = Append Line to end of data 
;;               Line   - OPTIONAL - 1-based line number to operate on. Required for all  
;;                                   actions except Append (9). 
;;               D1     - OPTIONAL - The first data element. Content varies with Action. 
;;               D2     - OPTIONAL - The second data element. Content varies with Action. 
;; 
;;REMARKS         
;; 
;;RETURNS         
;; 
;;DEPENDENCIES   none 
;; 
;;TESTED WITH    W2K8, W2K12, W2K16 
;; 
;;EXAMPLES        
; 
Function Edit($_Data, $_Action, OPTIONAL $_Line, $_D1, $_D2)
 
  Dim $_S							; String flag 
  Dim $_L, $_R							; String manipulation vars 
  Dim $_I							; Index pointer 
 
  $_S = 0
 
  If VarType($_Data) < 8192					; if Data is not an array 
    $_S = 1							; set String flag 
    $_Data = Split($_Data, Chr(10))				; convert to Array 
  EndIf
 
  If $_Action < 6
    $_Line = $_Line - 1						; set Line to zero-based 
    If $_Line < 0
      Exit 87							; exit error if < 0 
    EndIf
  EndIf
 
  $_Action = 0 + $_Action					; Force numeric 
 
  Select
   Case $_Action = 0						; Replace Line 
    $_Data[$_Line] = $_D1
   Case $_Action = 1						; Insert D1 at D2 
    If Left($_D2, 1) = '#'
      $_L = Val(SubStr($_D2, 2))				; defined by position 
      If $_L > Len($_Data[$_Line])
        $_L = $_Data[$_Line]
        $_R = ''
      Else
        $_R = SubStr($_Data[$_Line], 1 + $_L)
        $_L = Left($_Data[$_Line], $_L)
      EndIf
      $_Data[$_Line] = $_L + $_D1 + $_R
    Else
      $_L = InStr($_Data[$_Line], $_D2)				; defined by text match 
      If $_L >= 1
        $_R = SubStr($_Data[$_Line], $_L)			; match - insert where found 
        $_L = Left($_Data[$_Line], $_L - 1)
      Else
        $_R = $_Data[$_Line]					; no match - insert at beginning 
        $_L = ''
      EndIf
      $_Data[$_Line] = $_L + $_D1 + $_R
    EndIf
    
   Case $_Action = 2						; Append D1 after D2 
    $_L = InStr($_Data[$_Line], $_D2)				; defined by text match 
    If $_L >= 1
      $_L = $_L + Len($_D2)
      $_R = SubStr($_Data[$_Line], $_L)				; match - insert where found 
      $_L = Left($_Data[$_Line], $_L - 1)
    Else
      $_R = $_Data[$_Line]					; no match - insert at beginning 
      $_L = ''
    EndIf
    $_Data[$_Line] = $_L + $_D1 + $_R
   
 
   Case $_Action = 3						; Replace D1 with D2 
    $_Data[$_Line] = Replace($_Data[$_Line], $_D1, $_D2)
    
   Case $_Action = 4						; Remove D1 
    $_Data[$_Line] = Replace($_Data[$_Line], $_D1, '')
    
   Case $_Action = 5						; Insert Line 
    ReDim Preserve $_Data[1 + UBound($_Data)]
    For $_I = UBound($_Data) to $_Line Step -1
      $_Data[$_I] = $_Data[$_I - 1]
    Next
    $_Data[$_Line] = $_D1
 
   Case $_Action = 6						; Delete Line 
    For $_I = $_Line To UBound($_Data) - 1
      $_Data[$_I] = $_Data[$_I + 1]
    Next
    ReDim Preserve $_Data[UBound($_Data) - 1]
 
   Case $_Action = 9                                            ; Append Line 
    ReDim Preserve $_Data[1 + UBound($_Data)]
    $_Data[UBound($_Data)] = $_D1
 
   Case 1
    ; No-Op 
 
  EndSelect
 
  If $_S							; string flag set - join array 
    $_Data = Join($_Data, Chr(10))				; return as string 
  EndIf
 
  $Edit = $_Data
 
  Exit 0
 
EndFunction