Jump to content

insert in 2 demensional array


Recommended Posts

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
Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

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