Jump to content

2DArrayInsertRow


Recommended Posts

Func _2DArrayInsertRow(ByRef $avArray, $iRow)
    If Not IsArray($avArray) Then Return SetError(1, 0, 0)
    If UBound($avArray, 0) <> 2 Then Return SetError(2, 0, 0)

    ; Add 1 row to the array
    Local $iUBoundRow = UBound($avArray) + 1
    Local $iUBoundCol = UBound($avArray, 2)
    ReDim $avArray[$iUBoundRow][$iUBoundCol]

    ; Move all entries down until the specified position
    For $i = $iUBoundRow - 1 To $iRow + 1 Step -1
        For $j = 0 To $iUBoundCol - 1
            $avArray[$i][$j] = $avArray[$i - 1][$j]
        Next
    Next

    Return $iUBoundRow
EndFunc   ;==>_2DArrayInsertRow

This function is very useful. My problem is i need to add more that 1 row at the specific position. Someone can help me to add the parameter of the number of rows to add?

Thanks

 

EDIT: I need to add an array of values (at the specific position) and i want to avoid to redim 30 times

Edited by HeyTom
Link to comment
Share on other sites

It was not so difficult...

#Include <Array.au3>

Local $a[3][2] = [[0,1],[10,20],[100,200]]
_ArrayDisplay($a)

_2DArrayInsertRow($a, 1, 3)
_ArrayDisplay($a)


Func _2DArrayInsertRow(ByRef $avArray, $iRow, $n)
    If Not IsArray($avArray) Then Return SetError(1, 0, 0)
    If UBound($avArray, 0) <> 2 Then Return SetError(2, 0, 0)

    ; Add $n rows to the array, start at $iRow
    Local $iUBoundRow = UBound($avArray) + $n
    Local $iUBoundCol = UBound($avArray, 2)
    ReDim $avArray[$iUBoundRow][$iUBoundCol]

    ; Move all entries down until the specified position
    For $i = $iUBoundRow - 1 To $iRow + $n Step -1
        For $j = 0 To $iUBoundCol - 1
            $avArray[$i][$j] = $avArray[$i - $n][$j]
            $avArray[$i - $n][$j] = ""
        Next
    Next

    Return $iUBoundRow
EndFunc   ;==>_2DArrayInsertRow

For the EDIT, you might work a bit by yourself  :)

Link to comment
Share on other sites

  • Moderators

HeyTom,

Why not use the _ArrayInsert function - that is why I wrote it:

#include <Array.au3>
#include <String.au3>

; Create original array
Local $aArray[6][3]
For $i = 0 To 5
    For $j = 0 To 2
        $aArray[$i][$j] = $i & " - " & $j
    Next
Next
_ArrayDisplay($aArray, "2D - Original")

; Create array to insert
Local $aFill[][] = [["New", "2 - 1", "2 - 2"], ["New", "3 - 1", "3 - 2"], ["New", "4 - 1", "4 - 2"], ["New", "5 - 1", "5 - 2"]]

; Create delimited string to show where to insert - we insert each time at line 2
$sRange = _StringRepeat("2;", UBound($aFill))
$sRange = StringTrimRight($sRange, 1)

; And insert it
_ArrayInsert($aArray, $sRange, $aFill)

_ArrayDisplay($aArray, "2D data with row delim string range")

M23

Edited by Melba23
Forgot to remove debugging lines

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

3 hours ago, HeyTom said:

i'll check it out by myself

Hmm, confined at home I can do that - a slight variation of the previous script

#Include <Array.au3>

Local $a[3][2] = [[1,2],[10,20],[100,200]]
Local $b[3][2] = [["a","b"],["c","d"],["e","f"]]

;_ArrayDisplay($a)
;_ArrayDisplay($b)

_2DArrayInsert2DArray($a, $b, 2)
_ArrayDisplay($a)


Func _2DArrayInsert2DArray(ByRef $avArray, $2Darray, $iRow)
    If Not IsArray($avArray) Then Return SetError(1, 0, 0)
    If UBound($avArray, 0) <> 2 Then Return SetError(2, 0, 0)
    If Not IsArray($2Darray) Then Return SetError(3, 0, 0)
    If UBound($2Darray, 0) <> 2 Then Return SetError(4, 0, 0)
    If $iRow < 0 or $iRow > UBound($avArray) Then Return SetError(5, 0, 0)

    ; Add $n rows to the array, start at $iRow
    Local $n = UBound($2Darray)
    Local $iUBoundRow = UBound($avArray) + $n
    Local $iUBoundCol = UBound($avArray, 2)
    ReDim $avArray[$iUBoundRow][$iUBoundCol]

    ; Move all entries down until the specified position & add the new rows
    For $i = $iUBoundRow - 1 To $iRow Step -1
        For $j = 0 To $iUBoundCol - 1
        $avArray[$i][$j] = ($i >= $iRow + $n) ? $avArray[$i - $n][$j] : $2Darray[$i - $iRow][$j]
        Next
    Next
EndFunc

Melba, I never thought of using _ArrayInsert to insert a whole 2D array into an other one  :>

Edited by mikell
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...