HeyTom Posted March 23, 2020 Share Posted March 23, 2020 (edited) 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 March 23, 2020 by HeyTom Link to comment Share on other sites More sharing options...
mikell Posted March 23, 2020 Share Posted March 23, 2020 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 HeyTom 1 Link to comment Share on other sites More sharing options...
HeyTom Posted March 23, 2020 Author Share Posted March 23, 2020 (edited) Thanks, yes i'll check it out by myself Edited March 23, 2020 by HeyTom Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted March 23, 2020 Moderators Share Posted March 23, 2020 (edited) 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 March 23, 2020 by Melba23 Forgot to remove debugging lines iamtheky 1 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
mikell Posted March 23, 2020 Share Posted March 23, 2020 (edited) 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 March 23, 2020 by mikell Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now