jhm0615

swapping just two elements in 2d array

10 posts in this topic

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?

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

You're swapping index 0 with index 0. What exactly were you expecting it to do?

Share this post


Link to post
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)

 

Share this post


Link to post
Share on other sites

InunoTaishou - I should be swapping 0,0 with 0,1 but that is not happening

JohnOne - Thanks I am doing something similar to that now.

czardas - That works to some extent but try to move element 3,2 to 3,8 it does not seem to work. In fact I can move 3,3 to 3,4 - 3,9 but I cannot move 3,0 - 3,2 to any other element.

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

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

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

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Actually there were some error checks missing from what I posted earlier. I have just added them.

Edited by czardas

Share this post


Link to post
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

1 person likes this

Share this post


Link to post
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