Jump to content

swapping just two elements in 2d array


Recommended Posts

I am trying to use the _arrayswap function to switch two individual elements of a 2d array. In a previous revision I could do this but in v3.3.14.2 I cannot.

This is the sample script that I am using to learn this function while it does not give me an error it does not perform at all

#include <Array.au3>

Local $aArray[10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Local $aArray_Base[10][10]
For $i = 0 To 9
    For $j = 0 To 9
        $aArray_Base[$i][$j] = $i & " - " & $j
    Next
Next
_ArrayDisplay($aArray_Base, "Original", Default, 8)
$aArray = $aArray_Base
_ArraySwap($aArray, 0, 0, False, 0, 1)
_ArrayDisplay($aArray, "Swapped cells 3-3, cells 3-4", Default, 8)

 

Has anyone been able to do this?

Link to comment
Share on other sites

#include <Array.au3>

Local $aArray[10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Local $aArray_Base[10][10]
For $i = 0 To 9
    For $j = 0 To 9
        $aArray_Base[$i][$j] = $i & " - " & $j
    Next
Next
_ArrayDisplay($aArray_Base, "Original", Default, 8)
$aArray = $aArray_Base
$tmp = $aArray[3][3]
$aArray[3][3] = $aArray_Base[3][4]
$aArray_Base[3][4] = $tmp
;_ArraySwap($aArray, 0, 0, False, 0, 1)
_ArrayDisplay($aArray, "Swapped cells 3-3, cells 3-4", Default, 8)
_ArrayDisplay($aArray_Base, "Swapped cells 3-4, cells 3-3", Default, 8)

 

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

#include <Array.au3>

Local $aArray[10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Local $aArray_Base[10][10]
For $i = 0 To 9
    For $j = 0 To 9
        $aArray_Base[$i][$j] = $i & " - " & $j
    Next
Next
_ArrayDisplay($aArray_Base, "Original", Default, 8)
$aArray = $aArray_Base
_ArraySwap($aArray, 3, 4, True, 3, 3)
_ArrayDisplay($aArray, "Swapped cells 3-3, cells 3-4", Default, 8)

 

Link to comment
Share on other sites

Yeah, the function isn't geared up to do that, but it's an easy function to create. The method was demonstrated earlier by JohnOne.

#include <Array.au3>

Local $aArray[10] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Local $aArray_Base[10][10]
For $i = 0 To 9
    For $j = 0 To 9
        $aArray_Base[$i][$j] = $i & " - " & $j
    Next
Next
_ArrayDisplay($aArray_Base, "Original", Default, 8)
$aArray = $aArray_Base
_SwapElements2D($aArray, 3, 2, 4, 3)
;_ArraySwap($aArray, 3, 4, True, 3, 3)
_ArrayDisplay($aArray, "Swapped cells 3-3, cells 3-4", Default, 8)

Func _SwapElements2D(ByRef $aArray, $iRow1, $iCol1, $iRow2, $iCol2)
    If Not IsArray($aArray) Or Ubound($aArray, 0) <> 2 Then Return SetError(1)
    If $iRow1 < 0 Or $iRow2 < 0 Or $iCol1 < 0 Or $iCol2 < 0 Then Return SetError(2)
    Local $iMaxRow = UBound($aArray) -1, $iMaxCol = UBound($aArray, 2) -1
    If $iRow1 > $iMaxRow Or $iRow2 > $iMaxRow Or $iCol1 > $iMaxCol Or $iCol2 > $iMaxCol Then Return SetError(3)

    Local $vTemp = $aArray[$iRow1][$iCol1]
    $aArray[$iRow1][$iCol1] = $aArray[$iRow2][$iCol2]
    $aArray[$iRow2][$iCol2] = $vTemp
EndFunc ;==> _SwapElements2D

 

Edited by czardas
Link to comment
Share on other sites

3 parameters instead of 5?  but none of the error checking.

#include<array.au3>
local $a = [["00" , "01" , "02"],["10" , "11" , "12"],["20" , "21" , "22"]]

_ArrayDisplay($a)

$aSwap = _2D_swap($a , "2,1" , "0,2")

_ArrayDisplay($aSwap)

Func _2D_swap($array , $2d1 , $2d2)

    $vTmp = $array[stringsplit($2d1 , "," , 2)[0]][stringsplit($2d1 , "," , 2)[1]]
    $array[stringsplit($2d1 , "," , 2)[0]][stringsplit($2d1 , "," , 2)[1]] = $array[stringsplit($2d2 , "," , 2)[0]][stringsplit($2d2 , "," , 2)[1]]
    $array[stringsplit($2d2 , "," , 2)[0]][stringsplit($2d2 , "," , 2)[1]] = $vTmp

return $array

EndFunc

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

A universal Swap-func swapping 2 Values from each var, arraytype: 

#include<array.au3>
local $a = [["00" , "01" , "02"],["10" , "11" , "12"],["20" , "21" , "22"]]

_ArrayDisplay($a)

_swap($a[2][1] , $a[0][2])

_ArrayDisplay($a)

Func _swap(ByRef $1 , ByRef $2)
    Local $Tmp = $1
    $1=$2
    $2=$Tmp
EndFunc

also 2 elements of 2D array

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...