Sign in to follow this  
Followers 0
Shao

SUPPRESSION OF AN ELEMENT IN A BIDIMENSIONAL ARRAY

7 posts in this topic

; #######################################

;

; SUPPRESSION OF AN ELEMENT IN A BIDIMENSIONAL ARRAY

;

; #######################################

Func BiDim_ArrayDelete(ByRef $array, $col2, $pos)

Local $i = 1, $j = 1

For $i = 1 To $array[0][0]

If Not($i=$pos) Then

Local $col=1

For $col = 1 To $col2

$array[$j][$col]=$array[$i][$col]

Next

$j = $j+1

EndIf

Next

ReDim $array[$array[0][0]][$col2+1]

EndFunc

Share this post


Link to post
Share on other sites



; #######################################
;
; SUPPRESSION OF AN ELEMENT IN A BIDIMENSIONAL ARRAY
;
; #######################################

Func BiDim_ArrayDelete(ByRef $array, $col2, $pos)
    Local $i = 1, $j = 1
    
    For $i = 1 To $array[0][0]
        If Not($i=$pos) Then
            Local $col=1
            For $col = 1 To $col2
                $array[$j][$col]=$array[$i][$col]
            Next
            $j = $j+1
        EndIf
    Next
    ReDim $array[$array[0][0]][$col2+1]
EndFunc
[ autoit] [ /autoit] Makes code easier to read

Nice code, but i don't understant how to use it

care to explain?

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi everybody,

I put a code above but I didn't take time to check it and anymore it's wrong and with no security in case of errors.

I was working all evening long to make it better and it works !

The good code just below :

; #############################

;

; SUPPRESSION OF A BIDIMENSIONAL ARRAY

;

; #############################

Func BiDim_ArrayDelete(ByRef $avArray, $col2, $pos)

Local $ArraySize = UBound($avArray) ; size of the original array

; TREATMENT OF ERRORS

; if the variable isn't an array

If Not(IsArray($avArray)) Then

SetError(1)

Return ""

EndIf

; if the array is only 1 element in size

If $ArraySize = 1 Then

SetError(2)

Return ""

EndIf

; if the element that must be deleted is higher or lower

If $pos > $ArraySize Or $pos < 0 Then

SetError(3)

Return ""

EndIf

; execution

Local $i = 1, $j = 1

Local $avNewArray[$ArraySize-1][$col2]

For $j = 1 To $ArraySize-1

If $j=$pos Then $j=$j+1

Local $col=0

For $col = 0 To $col2-1

$avNewArray[$i][$col]=$avArray[$j][$col]

Next

$i = $i+1

Next

$avArray = $avNewArray

SetError(0)

Return 1

EndFunc

Edited by Shao

Share this post


Link to post
Share on other sites

This code above is useful to suppress an element from a bidimensional array a _ArrayDelete() do it with unidimensional array.

How to use it :

just type

BiDim_ArrayDelete($My_BiDimensional_Array, $Col, $Element)

where :

$My_BiDimensional_Array is the name of your bidimensional array with no quote -as in _ArrayDelete()-

$My_BiDimensional_Array[$i][$j] when used in your code

$Col is the number of columns of your bidimensional array

$Element is the number of row to delete from your bidimensional array

for example, if your bidimensional array was a result of IniReadSection(), so it contains 2 columns and as many rows as there are in the ini file read.

example :

$MDP = IniReadSection($PARAM_INI, "PASSWORD")

BiDim_ArrayDelete($MDP, 2, 5)

in the above example, $MDP is a bidimensional array that contains all the passwords (15) contained in the section "PASSWORD" of an ini file.

So,

$MDP[0][0] = 15

$MDP[1][0] = key of the first row and $MDP[1][1] = value of the first row

etc. till 15

this variable has got 2 columns (first for key and second for value)

We declare name of array and number of columns as parameters of BiDim_ArrayDelete($MDP, 2, 5)

5 means that we want to delete the 5th row of the array $MDP[5][0] and $MDP[5][1]

In case of errors in parameters (array isn't modified) it returns "" as result and :

1 means the first parameter isn't an array

2 means the array's got only 1 element (so no necessary to delete it by using this function)

3 means the element to delete is <0 or higher than the last element of the array

Share this post


Link to post
Share on other sites

Ahh... I get it now

thats pretty cool

thanks for sharing

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Hi,

I had the need for a similar function; in fact in the signature there is a link to "Array2D.au3" with;

_ArrayDelete2D(ByRef $avArray, $nRow = '')
_ArrayDelete2DColumn(ByRef $avArray, $nRow = '')oÝ÷ Ù©Ý"wuçm¢Ê^yØ­ºè® yø¥zË(r§çb¶Þ®Ú/nÇ+ub«~éܶ*'±ç¦²ÚzW­zº0Íêèu«Zûayªëk(¬Íêèm«w`%²az«¨¶{¦mêè}Ê%ºiìªê-¥ªÚë^®+'¢ÙÞy×j̨¹Æ§­ëkëÞÜ"¶®¶­sgV&÷VæBb33c¶'&Ã"

Best, Randall

[Mind you, I have been slack with error handling; power to you!]

Edited by randallc

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

It's right that wasn't needed to use a number-of-columns parameter, so I modified the code :

;===============================================================================
;
; Function Name     : BiDim_ArrayDelete()
; Description       : Deletes the specified element from the given 2D_array
; Parameters        : 1st param. = the 2D_array passed via ByRef
;                     2nd param. = the element to delete
; Control of Errors : @Error = 1 if the 1st param. isn't an array
;                            = 2 if the array is only 1 element in size
;                            = 3 if the array isn't a 2D_array
;                            = 4 if the 2nd param. is <0 or > array size
; Return Value      : the adjusted 2D_array
; Author(s)         : Shao <dragonban@yahoo.fr>
;
;===============================================================================

Func BiDim_ArrayDelete(ByRef $avArray, $pos)
    
    Local $ArraySize = UBound($avArray)-1 ; size of the original array
    Local $ArrayCol = UBound($avArray, 2)
    Local $i = 1, $j = 1
    Local $avNewArray[$ArraySize][$ArrayCol]

    ; TREATMENT OF ERRORS
    ; if the variable isn't an array
    If Not(IsArray($avArray)) Then
        SetError(1)
        Return ""
    EndIf
    
    ; if the array is only 1 element in size
    If $ArraySize = 1 Then
        SetError(2)
        Return ""
    EndIf
    
    ; if the array isn't bidimensional
    If Not($ArrayCol=2) Then
        SetError(3)
        Return ""
    EndIf
    
    ; if the element that must be deleted is higher or lower
    If $pos > $ArraySize Or $pos < 0 Then
        SetError(4)
        Return ""
    EndIf

    ; execution
    For $j = 1 To $ArraySize
        If $j=$pos Then $j=$j+1
        Local $col=0
        For $col = 0 To $ArrayCol-1
            $avNewArray[$i][$col]=$avArray[$j][$col]
        Next
        $i = $i+1
    Next
$avArray = $avNewArray
SetError(0)
Return 1
EndFunc

Return Value :

Success : return the adjusted 2D_array

Failure :
@Error = 1 if the 1st param. isn't an array
         2 if the array is only 1 element in size
         3 if the array isn't a 2D_array
         4 if the 2nd param. is <0 or > array size
Edited by Shao

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
Sign in to follow this  
Followers 0