Jump to content

2D Array's ArrayFindAll ... then results added to 2D array ArrayAdd?


Go to solution Solved by czardas,

Recommended Posts

Good Evening AutoIT Gurus!

:ILA2:

Love the program - can't say enough good things about it. AutoIT's program is money and so is this website. My apologies for being such a newb... as my biggest issues usually are just not understanding loops and arrays... so, ya I have another 2D array question.

Let's say I have a 2D array wih lots of rows and columns and I search with ArrayFindAll.

ArrayFindAll returns 12,13,14. How can I add those ArrayFindAll "results" from the previous array's rows / columns to a new or existing 2d array?

I really hope this is a stupid question - and it probably is - but I really don't know and searching for ArrayFindAll and ArrayAdd doesn't produce any results and that (in my mind) is what I'd probably logically do. Find them, then add them - lol... Thanks Everyone!

All my names are in the 3rd column [3] - "$aSP_Names_Match01"

All the names I'm trying to match them to are in column [0]."$aSN_Names_Match01"

Yes, it's messy because I get all the data from IE tables... but it works so far :)

For $s14 = Ubound($aSP_Names_Match01) - 1 to 0 Step - 1
MsgBox(0, "SP Name Match", $aSP_Names_Match01[$s14][3]) ; Shows me a name I'm matching
$aelement = _ArrayFindAll ($aSN_Names_Match01, $aSP_Names_Match01[$s14][3] , 0, 0, 0, 1, 0) ; Finds all matches in column 0
_ArrayDisplay ($aelement) ; Shows 12,13,14 or whatever it finds...
; How Do I Add the 1D ArrayFindAll results (rows and columns) to a new or existing 2d Array?
Next

Thanks Everyone! If you need more information I can provide :)

:thumbsup:

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

  • Solution

Expanding on the helpfile example:

#include <Array.au3>

Local $avArray[5][2] = [["0","a"],["1","b"],["2","c"],["3","b"],["4","a"]]
Local $avResult = _ArrayFindAll($avArray, "a", 0, 0, 0, 0, 1)
_ArrayDisplay($avArray, "$avArray")
_ArrayDisplay($avResult, "Results of searching for 'a' in $avArray")

; The new array will have the same number of columns
Local $aNewArray[UBound($avResult)][UBound($avArray, 2)]

For $i = 0 To UBound($avResult) -1 ; Loop through the returned index numbers.
    For $j = 0 To UBound($avArray, 2) -1 ; Loop through each of the columns.
        $aNewArray[$i][$j] = $avArray [$avResult[$i]] [$j] ; Populate the new array.
    Next
Next

_ArrayDisplay($aNewArray, "New Array")
Link to comment
Share on other sites

Well that was pretty sweet - thank you czardas :ninja: I hate to be annoying or a pain...

but how do I add up all the _ArrayFindAll $avResult before I display the results?

I have several names which I'm searching for and I need all the results.

I'm doing a For... Next loop to do a ArrayFindAll on the names.

think I'm missing something from your above code... how do I add the ArrayFindAll's results again?

For $s14 = Ubound($aSP_Names_Match01) - 1 to 0 Step - 1

$avResult = _ArrayFindAll ($aSN_Names_Match01, $aSP_Names_Match01[$s14][3] , 0, 0, 0, 1, 0)

_ArrayDisplay($avResult, "Results of searching for " & $aSP_Names_Match01[$s14][3] & " in $aSN_Names_Match01")

; How do I add the ArrayFindAll $avResult the the next _ArrayFindAll $avResult?

; Something like this... but then again it's an array so...  $avResult += $avResult

Next

Sorry I'm a little fried and the help file is just a wweee bit too cheesy... Thanks! I'm going to continue to search the forums after I submit this - thanks again!

:sorcerer:

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

Well, I believe I have it... and my original subject title is not accurate to what I ended up really needing as ArrayFindAll was 1 dimensional and I just needed them added together for my last question. Here's what I did to add the ArrayFindAll's... yes, I'm soo green - my bad!

Local $avTotal[1] = ["Total"]

For $s14 = Ubound($aSP_Name_Match01) - 1 to 0 Step - 1 ; Goes through my names one at a time...

$avResult = _ArrayFindAll ($aSN_Name_Match01, $aSP_Name_Match01[$s14][3] , 0, 0, 0, 1, 0) ; Matches the names in SN column 0 with SP Column 3

_ArrayDisplay($avResult, "Results of searching for " & $aSP_Name_Match01[$s14][3] & " in $aSN_Name_Match01") ; Shows how many found

If $avResult <> -1 Then _ArrayConcatenate ($avTotal, $avResult) ; If it's found it will be added to the "Total" array

Next

_ArrayDisplay ($avTotal) ; Shows all the ArrayFindAll results in one array

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

Firstly I hope you understand the straight forward method of creating a 2D array using nested loops shown above. You can alter the size of your array using Redim (so that it can hold more entries), alternatively you can feed the results to a larger array using the same method I demonstrated above. The help file is not cheesy BTW (it's your best friend).

Link to comment
Share on other sites

  • 2 weeks later...

Sorry your right - the help file is money - I like the examples too - probably between the computer and chair is cheesy - sorry and thanks again!

Edited by souldjer777

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

This is how to do a 1D array after arrayfindall - I know now thanks to czardas :graduated:

ArrayFindAll gets all indices then only sends those results into a new array

Local $avResult = _ArrayFindAll($SP_Unique_Min, $MinButton01_Match01, 0, 0, 0, 1, 1)

_ArrayDisplay($SP_Unique_Min, "$avArray")

_ArrayDisplay($avResult, "Results of searching for $MinButton01_Match01 in $avArray")

; The new array will have the same number of columns

Local $aNewArray[UBound($avResult)]

For $i = 0 To UBound($avResult) -1 ; Loop through the returned index numbers.
$aNewArray[$i] = $SP_Unique_Min[$avResult[$i]] ; Populate the new array.
Next

_ArrayDisplay($aNewArray, "New Array")

My head hurts...

:P

Edited by souldjer777

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

Link to comment
Share on other sites

  • 2 years later...

I just wanted to say thank you again to

czardas

 

:graduated:  He gave me the answer to a question I had 3 years earlier and I didn't give him the recognition he deserved. Sorry. You answered this very same question I proposed today in the forums... :idiot:

:guitar:  You rock.  Thank you very much.  Your an AutoIT Ninja! :ninja:

You solved my problem I had today... yes three years later... and yes thank you again!

'?do=embed' frameborder='0' data-embedContent>>

#RequireAdmin
#include <array.au3>
#include <file.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>

Global $WXYZArray01, $array01, $ProgramTitle, $sleeptime, $k01, $aExtract, $aArray

$ProgramTitle = "Testing 1 2 3"

$sleeptime = 1000

_FileReadToArray(@ScriptDir & "\" & "testing_output_csv_unique_values.csv", $array01, "", ",")

;_ArrayDisplay ($array01, "Array01")

$aUniqueHostname = _ArrayUnique ($array01, 1)

;_ArrayDisplay ($aUniqueHostname, "UniqueHostname ")

For $i01 = Ubound($aUniqueHostname) - 1 to 0 Step - 1
    For $j01 = Ubound($array01) - 1 to 0 Step - 1
        If $array01[$j01][1] == $aUniqueHostname[$i01] and StringRegExp($array01[$j01][5], "MY_OTHER_VALUE") then
            MsgBox(0, "Computer and MY_OTHER_VALUE",  $aUniqueHostname[$i01] & " : " & $array01[$j01][5])
            $FileName01 = @ScriptDir & "\" & $array01[$j01][3] & "_" & $aUniqueHostname[$i01] & "_" & $array01[$j01][2] & ".csv"
             MsgBox(0, "File Name", $FileName01)

                SplashTextOn($ProgramTitle, 'Generic - Please wait for loop to complete...', 400, 40, -1, -1, 2, "", 10)
                Sleep ($sleeptime)

                Local $avResult = _ArrayFindAll($array01, $aUniqueHostname[$i01], 0, 0, 0, 0, 1)
                _ArrayDisplay($avResult, "$avResult")
                Local $aNewArray[UBound($avResult)][UBound($array01, 2)]

                    For $i = 0 To UBound($avResult) -1 ; Loop through the returned index numbers.
                        For $j = 0 To UBound($array01, 2) -1 ; Loop through each of the columns.
                            $aNewArray[$i][$j] = $array01 [$avResult[$i]] [$j] ; Populate the new array.
                        Next
                    Next

                SplashOff()
                MsgBox (0, "Out of the loop", "Out of the loop - File Write From Array")
                _FileWriteFromArray($FileName01, $aNewArray, 1)

            ExitLoop
        EndIf
    Next
Next

"Maybe I'm on a road that ain't been paved yet. And maybe I see a sign that ain't been made yet"
Song Title: I guess you could say
Artist: Middle Class Rut

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