Jump to content

Leaderboard

Popular Content

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

  1. Yea .... that makes sense !
    1 point
  2. My version uses only ArrayToString, ArrayFromString and string functions. It won't rotate if the default delimiter is found in the array. #include <Array.au3> Local $aArray[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] _ArrayDisplay($aArray, "BEFORE rotation") If _ArrayRotate($aArray, 4) Then _ArrayDisplay($aArray, "AFTER rotation") Else MsgBox(Default, Default, "Array did not rotate!") EndIf Exit Func _ArrayRotate(ByRef $aArray, $iMakeFirst) ; $aArray = array to be rotated ; $iMakeFirst = element number to be rotated into position [0] ; Returns True if successful, False if not If Not IsArray($aArray) Then Return False ; invalid array If ($iMakeFirst < 0) Or ($iMakeFirst > UBound($aArray) - 1) Then Return False ; $iMakeFirst parameter is out of bounds If $iMakeFirst = 0 Then Return True ; no rotation needed, it's already the first element If StringInStr(_ArrayToString($aArray, ""), "|") Then Return False ; default delimiter found in array Local $sArray = _ArrayToString($aArray) Local $iPos = StringInStr($sArray, "|", $STR_NOCASESENSE, $iMakeFirst) Local $sFirst = StringLeft($sArray, $iPos - 1) Local $sLast = StringRight($sArray, StringLen($sArray) - $iPos) $aArray = _ArrayFromString($sLast & "|" & $sFirst) Return True EndFunc ;==>_ArrayRotate
    1 point
  3. Leave the array alone and access its elements using an offset and Mod() the array size. Local $aOrig = [0,1,2,3,4,5,6,7,8,9] Func ArrOfs(ByRef $a, $iOfs, $iIdx) Return($a[Mod($iOfs + $iIdx, UBound($a))]) EndFunc ConsoleWrite("New element of $aOrig at index 0 is " & ArrOfs($aOrig, 4, 0) & @LF) ConsoleWrite("New element of $aOrig at index 7 is " & ArrOfs($aOrig, 4, 7) & @LF) ; if you really need to permanently change the array, elements index offset by $n: Func ArrMove(ByRef $a, $n) Local $iSize = UBound($a) Local $b[$iSize] For $i = 0 To $iSize - 1 $b[$i] = $a[Mod($i + $n, $iSize)] Next $a = $b EndFunc ArrMove($aOrig, 4) _ArrayDisplay($aOrig)
    1 point
  4. Then this one liner : #include <Array.au3> #include <String.au3> Local $avArrayTarget[] = [1, 2, 3, 4, 5, 6, 7, 8, 9] Local $iShift = 4 $arr = StringSplit(StringTrimRight(StringRegExpReplace(_ArrayToString($avArrayTarget, ","), "(" & _StringRepeat(".+?,", $iShift) & ")(.+)", "$2,$1"), 1), ",", $STR_NOCOUNT) _ArrayDisplay($arr)
    1 point
  5. @TimRude to do that, I scripted recently a function named _ArrayRowMove in this post. Hope it helps
    1 point
  6. Here's an alternative using _ArrayPush -- #include <Array.au3> Local $avArrayTarget[] = [1, 2, 3, 4, 5, 6, 7, 8, 9] _ArrayDisplay($avArrayTarget, "BEFORE rotation") _ArrayRotate($avArrayTarget, 4) _ArrayDisplay($avArrayTarget, "AFTER rotation") Func _ArrayRotate(ByRef $aArray, $iMakeFirst) ; $aArray = array to be rotated ; $iMakeFirst = element number to be rotated into position [0] ; Returns True if successful, False if not If Not IsArray($aArray) Then Return False ; invalid array If ($iMakeFirst < 0) Or ($iMakeFirst > UBound($aArray) - 1) Then Return False ; $iMakeFirst parameter is out of bounds If $iMakeFirst = 0 Then Return True ; no rotation needed, it's already the first element Local $aFirst = _ArrayExtract($aArray, 0, $iMakeFirst - 1) Local $iResult = _ArrayPush($aArray, $aFirst) Return ($iResult = 1) EndFunc
    1 point
×
×
  • Create New...