Jump to content

# Help with _2DArrayDelete

## Recommended Posts

I noticed that the Array UDF does not have support for 2D arrays. So I wrote this simple version of 2DArrayDelete.

This is an example of my 2D array I'm using:

```;Pseudo code
Dim array[3][3]
;where as the second dimension's size never changes, its a set of variables. the size
;of the first dimension is the dynamic one. sort of a pseudo object list
array[0] = {var1,var2,var3};second dimension indexes are 0, 1, 2
array[1] = {var1,var2,var3}
array[2] = {vara,varb,varc}```

```Func _2DArrayDelete(ByRef \$avArray,\$iElement)
Local \$i1DCntr = 0, \$i2DCntr = 0, \$i1DUpper = 0, \$i2DUpper = 0

If (Not IsArray(\$avArray)) Then
SetError(1)
Return ""
EndIf

; We have to define this here so that we're sure that \$avArray is an array
; before we get it's size.
\$i1DUpper = UBound(\$avArray)    ; Size of original 1st dimension array
\$i2DUpper = UBound(\$avArray,2)  ; Size of original 2nd dimension array

; If the array is only 1 element in size then we can't delete the 1 element.
If \$i1DUpper = 1 Then
SetError(2)
Return ""
EndIf

Local \$avNewArray[\$i1DUpper - 1][\$i2DUpper] ;minus one to the first dimension and preserve the second

For \$i1DCntr = 0 To (\$i1DUpper - 1)
If \$i1DCntr = \$iElement Then
ContinueLoop
EndIf
For \$i2DCntr = 0 To (\$i2DUpper - 1)
\$avNewArray[\$i1DCntr][\$i2DCntr] = \$avArray[\$i1DCntr][\$i2DCntr]
Next
Next
\$avArray = \$avNewArray
SetError(0)
Return 1
EndFunc```

I get the error:

```==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:
\$avNewArray[\$i1DCntr][\$i2DCntr] = \$avArray[\$i1DCntr][\$i2DCntr]
^ ERROR```

#### Share this post

##### Share on other sites

```#include <array.au3>

Dim \$array[3][3]

For \$i = 0 to 2
For \$j = 0 to 2
\$array[\$i][\$j] = \$i & ' ' & \$j
Next
Next

_ArrayDisplay(\$array)

_2DArrayDelete(\$array, 1)

_ArrayDisplay(\$array)

Func _2DArrayDelete(ByRef \$avArray,\$iElement)
Local \$i1DCntr = 0, \$i2DCntr = 0, \$i1DUpper = 0, \$i2DUpper = 0, \$pastElement = 0

If (Not IsArray(\$avArray)) Then
SetError(1)
Return ""
EndIf

; We have to define this here so that we're sure that \$avArray is an array
; before we get it's size.
\$i1DUpper = UBound(\$avArray)   ; Size of original 1st dimension array
\$i2DUpper = UBound(\$avArray,2) ; Size of original 2nd dimension array

; If the array is only 1 element in size then we can't delete the 1 element.
If \$i1DUpper = 1 Then
SetError(2)
Return ""
EndIf

Local \$avNewArray[\$i1DUpper - 1][\$i2DUpper];minus one to the first dimension and preserve the second

For \$i1DCntr = 0 To (\$i1DUpper - 1)
If \$i1DCntr = \$iElement Then
\$pastElement = 1
Else
For \$i2DCntr = 0 To (\$i2DUpper - 1)
\$avNewArray[\$i1DCntr - \$pastElement][\$i2DCntr] = \$avArray[\$i1DCntr][\$i2DCntr]
Next
EndIf
Next
\$avArray = \$avNewArray
SetError(0)
Return 1
EndFunc```
I think this works...

What I added wast the \$pastElement variable. If they loop passes the element then all the things after that need to be assigned one before what they are so they move up one.

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

#### Share this post

##### Share on other sites

Oh! I see now, forgot to minute one once I skipped over the one to remove. Thanks.

#### Share this post

##### Share on other sites

This is actually more efficient. It starts at the index to be removed and then goes. It also doesn't create a new array but just ReDim's the original array.

```#include <array.au3>

Dim \$array[3][3]

For \$i = 0 to 2
For \$j = 0 to 2
\$array[\$i][\$j] = \$i & ' ' & \$j
Next
Next

_ArrayDisplay(\$array)

_2DArrayDelete(\$array, 1)

_ArrayDisplay(\$array)

Func _2DArrayDelete(ByRef \$avArray,\$iElement)
Local \$i1DCntr = 0, \$i2DCntr = 0, \$i1DUpper = 0, \$i2DUpper = 0

If (Not IsArray(\$avArray)) Then
SetError(1)
Return ""
EndIf

; We have to define this here so that we're sure that \$avArray is an array
; before we get it's size.
\$i1DUpper = UBound(\$avArray)   ; Size of original 1st dimension array
\$i2DUpper = UBound(\$avArray,2) ; Size of original 2nd dimension array

; If the array is only 1 element in size then we can't delete the 1 element.
If \$i1DUpper = 1 Then
SetError(2)
Return ""
EndIf

For \$i1DCntr = \$iElement + 1 To (\$i1DUpper - 1)
For \$i2DCntr = 0 To (\$i2DUpper - 1)
\$avArray[\$i1DCntr - 1][\$i2DCntr] = \$avArray[\$i1DCntr][\$i2DCntr]
Next
Next

ReDim \$avArray[\$i1DUpper - 1][\$i2DUpper]

SetError(0)
Return 1
EndFunc```

My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]

#### Share this post

##### Share on other sites

Good point on that one. Thanks again.

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

×

• Wiki

• Back

• Git

## Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.