Jump to content
Sign in to follow this  
Allow2010

insert in 2 demensional array

Recommended Posts

Allow2010

Hi,

i am using a two dimensional array like this:

Dim $formfields[3][2] = [["element1", "data1"],["element2", "data2"],["elemnt0","data0"]]

there can be x elements.

My problem is that i need to have a certain element at the first position (the order of the other elements does not matter, only on certain element hast to be at position 1).

Dim $formfields[3][2] = [["element0", "data0"],["element1", "data1"],["elemnt2","data2"]]

I wanted to check if a certain element exist in formfields and if it exists, i remove it and re insert it at position 1. If it does not exist, i just want to add it at position 1.

I tried several things, but i could not get this done correctly, due to problems with the array dimensions after removing/adding...

Can anyone show me how to do it right?

thanks in adnvance,

Edit:

here is what already works:

Func _AddOrUpdateFieldIn2DArray($array, $fieldname, $data = "")
    $iIndex = _ArraySearch($array, $fieldname, 0, 0, 1, 2, 1, 0)
    If $iIndex <> -1 Then;the field exists
        If $array[$iIndex][1] = $data Then          
                      ;already up to date
        Else
            ;update
                     $array[$iIndex][1] = $data
        EndIf
    Else
            ;add
            ReDim $array[UBound($array) + 1][2]
        $array[UBound($array) - 1][0] = $fieldname
        $array[UBound($array) - 1][1] = $data
    EndIf
    Return $array
EndFunc

and would like to have something like this also:

Func _AddTobeginningOf2DArray($array, $fieldname, $data = "")
;adds $fieldname to the beginning of the array, removes $filedname from other positions if it already exists

    Return $array
EndFunc
Edited by Allow2010

Share this post


Link to post
Share on other sites
water

You could have a look at function _ArrayInsert from the Array UDF that comes with AutoIt. The function only works for one-dimensional arrays but it shouldn't be too hard to modify the code.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Allow2010

Func _ArrayInsert(ByRef $avArray, $iElement, $vValue = "")

    ; Add 1 to the array
    Local $iUBound = UBound($avArray) + 1
    ReDim $avArray[$iUBound]

    ; Move all entries over til the specified element
    For $i = $iUBound - 1 To $iElement + 1 Step -1
        $avArray[$i] = $avArray[$i - 1]
    Next

    ; Add the value in the specified element
    $avArray[$iElement] = $vValue
    Return $iUBound
EndFunc   ;==>_ArrayInsert

i played with this before, but failed....i will try again...

Share this post


Link to post
Share on other sites
water

Or search the forum for 2D arrays. Some users have enhanced the Array UDF so it works fine with 2D arrays.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

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  

×