Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 12/10/2022 in all areas

  1. Deye

    Move rows on 2D array

    #include <Array.au3> Dim $aArray[][] = [["A", 0], ["B", 0], ["C", 0], ["D", 0], ["E", 1], ["F", 1]] _ArrayDisplay(_ArrPopToTop($aArray, "B|D|F")) _ArrayDisplay(_ArrPopToTop($aArray, "D|E")) _ArrayDisplay(_ArrPopToTop($aArray, "C")) Func _ArrPopToTop($a, $val = "", $index = 0) Local $asplit = StringSplit($val, "|", 3) _ArrayReverse($asplit) For $j = 0 To UBound($asplit) - 1 $x = _ArraySearch($a, $asplit[$j], "", "", "", 0) _ArrayInsert($a, $index, _ArrayExtract($a, $x, $x)) _ArrayDelete($a, $x + 1) Next Return $a EndFunc
    1 point
  2. pixelsearch

    Move rows on 2D array

    You could also script a function reusable in similar cases (e.g. move a block of rows to top or bottom of a 1D/2D array), let's name the function _ArrayRowMove : #Include <Array.au3> #Include <MsgBoxConstants.au3> ; Local $aArray[] = ["A0", "B1", "C2", "D3", "E4", "F5", "G6", "H7"] ; or Local $aArray[][] = [["A",0],["B",1],["C",2],["D",3],["E",4],["F",5],["G",6],["H",7]] _ArrayDisplay($aArray, "before move") _ArrayRowMove($aArray, 2, 5) ; rows 2 to 5 moved to top ; or ; _ArrayRowMove($aArray, 2, 5, False) ; rows 2 to 5 moved to bottom If @error Then Msgbox($MB_TOPMOST, "_ArrayRowMove", "error = " & @error) Else _ArrayDisplay($aArray, "after move") EndIf ;================================================================== Func _ArrayRowMove(ByRef $aArray, $iStart, $iEnd, $bTopDest = True) If Not IsArray($aArray) Then Return SetError(1, 0, 0) Local $aBackup, $iDims, $iRows, $iCols, $iInterval, $iInc = -1 $iDims = UBound($aArray, $UBOUND_DIMENSIONS) $iRows = UBound($aArray, $UBOUND_ROWS) $iCols = UBound($aArray, $UBOUND_COLUMNS) ; always 0 for 1D array (+++) If $iDims < 1 Or $iDims > 2 Then Return SetError(2, 0, 0) If $iRows = 0 Then Return SetError(3, 0, 0) ; not sure about this test If $iStart = Default Or $iStart < 0 Then $iStart = 0 If $iEnd = Default Or $iEnd > $iRows - 1 Then $iEnd = $iRows - 1 $iInterval = $iEnd - $iStart If $iInterval < 0 Then Return SetError(4, 0, 0) $aBackup = $aArray If $iDims = 1 Then ; 1D If $bTopDest Then ; move to top For $i = $iStart -1 To 0 Step -1 $iInc += 1 ; 0+ $aArray[$iEnd - $iInc] = $aBackup[$i] Next For $i = 0 To $iInterval $aArray[$i] = $aBackup[$iStart + $i] Next Else ; move to bottom For $i = $iEnd +1 To $iRows -1 $iInc += 1 ; 0+ $aArray[$iStart + $iInc] = $aBackup[$i] Next $iInc = -1 For $i = $iRows -1 - $iInterval To $iRows -1 $iInc += 1 ; 0+ $aArray[$i] = $aBackup[$iStart + $iInc] Next EndIf Else ; 2D If $bTopDest Then ; move to top For $i = $iStart -1 To 0 Step -1 $iInc += 1 ; 0+ For $j = 0 To $iCols -1 $aArray[$iEnd - $iInc][$j] = $aBackup[$i][$j] Next Next For $i = 0 To $iInterval For $j = 0 To $iCols -1 $aArray[$i][$j] = $aBackup[$iStart + $i][$j] Next Next Else ; move to bottom For $i = $iEnd +1 To $iRows -1 $iInc += 1 ; 0+ For $j = 0 To $iCols -1 $aArray[$iStart + $iInc][$j] = $aBackup[$i][$j] Next Next $iInc = -1 For $i = $iRows -1 - $iInterval To $iRows -1 $iInc += 1 ; 0+ For $j = 0 To $iCols -1 $aArray[$i][$j] = $aBackup[$iStart + $iInc][$j] Next Next EndIf EndIf Return 1 ; success EndFunc Update Dec 23, 2022 * Added code for 1D array * Added error checking
    1 point
×
×
  • Create New...