Jump to content

how to take an array result and populate new array


kor
 Share

Go to solution Solved by water,

Recommended Posts

I'm drawing a blank here as I don't recall that I've ever had to do this exact thing.

Basically I'm doing an AD query where I get a 1 dimensional array of values that contains only 1 column.

I then need to do an additional query to lookup each user from the first array to get additional data about each user.

That part I already have figured out.

Where I'm stuck is trying to figure out how to take the result of each line from the first array and populate all the results into a single new array?

Does that make sense?

What I have so far. But it's not what I want because I get an array for EACH user, rather than 1 array containing the results of all users from the For Loop.

#include <AD.au3>
#include <Array.au3>

_AD_Open()

Global $sGroup = "IS Department" ; group name

$aResult = _AD_RecursiveGetGroupMembers($sGroup, 10, True, False)

_ArrayDisplay($aResult)

For $i = 1 To UBound($aResult) - 1
    $aResult1 = _AD_GetObjectProperties($aResult[$i], "displayname,description,physicalDeliveryOfficeName")
    _ArrayDisplay($aResult1)
Next

_AD_Close()
Link to comment
Share on other sites

Something like this - I know it is far from being beautiful, but it works:

#include <AD.au3>
#include <Array.au3>

_AD_Open()

Global $sGroup = "IS Department" ; group name

$aResult = _AD_RecursiveGetGroupMembers($sGroup, 10, True, False)

_ArrayDisplay($aResult)

For $i = 1 To UBound($aResult) - 1
    $aResult1 = _AD_GetObjectProperties($aResult[$i], "displayname,description,physicalDeliveryOfficeName")
    $sDN = ""
    $sDesc = ""
    $sDO = ""
    For $j = 1 To $aResult1[0][0]
        If $aResult1[$j][0] = "displayname" Then
            $sDN = $aResult1[$j][1]
        ElseIf $aResult1[$j][0] = "description" Then
            $sDesc = $aResult1[$j][1]
        ElseIf $aResult1[$j][0] = "physicalDeliveryOfficeName" Then
            $sDO = $aResult1[$j][1]
        EndIf
    Next
    $aResult[$i] = $aResult[$i] & "," & $sDN & "," & $sDesc & "," & $sDO
    _ArrayDisplay($aResult1)
Next
_ArrayDisplay($aResult)
_AD_Close()

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

 

Something like this - I know it is far from being beautiful, but it works:

#include <AD.au3>
#include <Array.au3>

_AD_Open()

Global $sGroup = "IS Department" ; group name

$aResult = _AD_RecursiveGetGroupMembers($sGroup, 10, True, False)

_ArrayDisplay($aResult)

For $i = 1 To UBound($aResult) - 1
    $aResult1 = _AD_GetObjectProperties($aResult[$i], "displayname,description,physicalDeliveryOfficeName")
    $sDN = ""
    $sDesc = ""
    $sDO = ""
    For $j = 1 To $aResult1[0][0]
        If $aResult1[$j][0] = "displayname" Then
            $sDN = $aResult1[$j][1]
        ElseIf $aResult1[$j][0] = "description" Then
            $sDesc = $aResult1[$j][1]
        ElseIf $aResult1[$j][0] = "physicalDeliveryOfficeName" Then
            $sDO = $aResult1[$j][1]
        EndIf
    Next
    $aResult[$i] = $aResult[$i] & "," & $sDN & "," & $sDesc & "," & $sDO
    _ArrayDisplay($aResult1)
Next
_ArrayDisplay($aResult)
_AD_Close()

What would you recommend to get each value to be its own column?

Stringsplit or other?

Link to comment
Share on other sites

The function above should return a 1D array and later you want to split each element?

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

Then StringSplit is the way to go.

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

Looping through the source array, Stringsplit an element and then use the resulting array to populate the target array.

If you can post an example of the source and the desired target I can provide the needed 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

Looping through the source array, Stringsplit an element and then use the resulting array to populate the target array.

If you can post an example of the source and the desired target I can provide the needed code.

#include <Array.au3>

Local $aArray[2][1] = [["mjones,job1,department1,office1"], ["jsmith,job2,department2,office2"]]

Local $aArray1[2][4] = [["mjones" ,"job1","department1","office1"], ["jsmith","job2","department2","office2"]]

_ArrayDisplay($aArray)
_ArrayDisplay($aArray1)
Link to comment
Share on other sites

  • Solution

#include <Array.au3>
#include <StringConstants.au3>

Local $aSource[2][1] = [["mjones,job1,department1,office1"], ["jsmith,job2,department2,office2"]]
Local $aTarget[2][4]

For $i = 0 To UBound($aSource, 1) - 1
    $aSplit = StringSplit($aSource[$i][0], ",", $STR_NOCOUNT)
    For $j = 0 To UBound($aSplit, 1) - 1
        $aTarget[$i][$j] = $aSplit[$j]
    Next
Next

_ArrayDisplay($aSource)
_ArrayDisplay($aTarget)

Edited by water

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

This version works for a 2D source array with only one column AND for a 1D array.

#include <Array.au3>
#include <StringConstants.au3>

; Local $aSource[2][1] = [["mjones,job1,department1,office1"], ["jsmith,job2,department2,office2"]]
Local $aSource[2] = ["mjones,job1,department1,office1", "jsmith,job2,department2,office2"]
Local $aTarget[2][4]
Local $iSourceDim = UBound($aSource, 0)

For $i = 0 To UBound($aSource, 1) - 1
    If $iSourceDim = 1 Then
        $aSplit = StringSplit($aSource[$i], ",", $STR_NOCOUNT)
    Else
        $aSplit = StringSplit($aSource[$i][0], ",", $STR_NOCOUNT)
    EndIf
    For $j = 0 To UBound($aSplit, 1) - 1
        $aTarget[$i][$j] = $aSplit[$j]
    Next
Next

_ArrayDisplay($aSource)
_ArrayDisplay($aTarget)

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

#include <AD.au3>
#include <Array.au3>

_AD_Open()

Global $sGroup = "ACC_TRAVEL" ; group name
Global $aMembers, $aUserProperties, $aSplit, $aFinal

$aMembers = _AD_RecursiveGetGroupMembers($sGroup, 10, True, False)

_ArrayDisplay($aMembers)

For $i = 1 To UBound($aMembers) - 1
    $aUserProperties = _AD_GetObjectProperties($aMembers[$i], "displayname,description,physicalDeliveryOfficeName")
    ;_ArrayDisplay($aUserProperties)
    Local $sDN = "", $sDesc = "", $sDO = ""
    For $n = 1 To $aUserProperties[0][0]
        If $aUserProperties[$n][0] = "displayname" Then
            $sDN = $aUserProperties[$n][1]
        ElseIf $aUserProperties[$n][0] = "description" Then
            $sDesc = $aUserProperties[$n][1]
        ElseIf $aUserProperties[$n][0] = "physicalDeliveryOfficeName" Then
            $sDO = $aUserProperties[$n][1]
        EndIf
    Next
    $aMembers[$i] = $aMembers[$i] & "," & $sDN & "," & $sDesc & "," & $sDO
Next
_ArrayDisplay($aMembers)
For $i = 1 To UBound($aMembers) - 1
    $aSplit = StringSplit($aMembers[$i], ",", 2)
    _ArrayDisplay($aSplit)
    For $n = 1 To UBound($aSplit, 1) - 1
        $aFinal[$i][$n] = $aSplit[$n]
    Next
Next

_ArrayDisplay($aFinal)

_AD_Close()

I get the following error:

(46) : ==> Subscript used on non-accessible variable.:

$aFinal[$i][$n] = $aSplit[$n]

$aFinal^ ERROR

 

EDIT: I can get this to work IF I define $aFinal as $aFinal[12][4]

However I can't always know how big or small the array is going to be. The output is dynamic and changes the number of columns depending on which group I point it at.

Edited by kor
Link to comment
Share on other sites

Where do you define $aFinal as an array?

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

The error simply means that $aFinal isn't an array when you try to access it.

Global $aTest
$atest[1] = "x"

Returns the same error messge.

Where do you use $aFinal in your script? Do you use it as a plain variable? Maybe a typo?

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

The error simply means that $aFinal isn't an array when you try to access it.

Global $aTest
$atest[1] = "x"

Returns the same error messge.

Where do you use $aFinal in your script? Do you use it as a plain variable? Maybe a typo?

My entire script is posted.

I define $aFinal as a variable at the top, and the first time I try and populate it is in the stringsplit loop.

If the $afinal array is declared at the top as $aFinal[12][4] then the entire script works fine. But what happens when I query a new group that has 50 members. or 4 members? I'm not sure what I'm doing wrong.

$aFinal is only there as a way for me to logical see the progression of the arrays in the code... so that I know that $aFinal is the final result of all the data and that is the one that I want to display.

Your original code got confusing when you have just generic 'source' and 'target', but there are multiple 'sources' since the data is moving between 3 different arrays.

EDIT: Entire code posted again.

#include <AD.au3>
#include <Array.au3>

_AD_Open()

Global $sGroup = "ACC_TRAVEL" ; group name
Global $aMembers, $aUserProperties, $aSplit, $aFinal[12][4]

$aMembers = _AD_RecursiveGetGroupMembers($sGroup, 10, True, False)

_ArrayDisplay($aMembers)

For $i = 1 To UBound($aMembers) - 1
    $aUserProperties = _AD_GetObjectProperties($aMembers[$i], "displayname,description,physicalDeliveryOfficeName")
    ;_ArrayDisplay($aUserProperties)
    Local $sDN = "", $sDesc = "", $sDO = ""
    For $n = 1 To $aUserProperties[0][0]
        If $aUserProperties[$n][0] = "displayname" Then
            $sDN = $aUserProperties[$n][1]
        ElseIf $aUserProperties[$n][0] = "description" Then
            $sDesc = $aUserProperties[$n][1]
        ElseIf $aUserProperties[$n][0] = "physicalDeliveryOfficeName" Then
            $sDO = $aUserProperties[$n][1]
        EndIf
    Next
    $aMembers[$i] = $aMembers[$i] & "," & $sDN & "," & $sDesc & "," & $sDO
Next
;_ArrayDisplay($aMembers)
For $i = 1 To UBound($aMembers) - 1
    $aSplit = StringSplit($aMembers[$i], ",", 2)
    ;_ArrayDisplay($aSplit)
    For $n = 0 To UBound($aSplit) - 1
        ;ConsoleWrite($i & "  ..  " & $aSplit[$n] & @CR)
        $aFinal[$i][$n] = $aSplit[$n]
    Next
Next
_ArrayDisplay($aFinal)

_AD_Close()
Edited by kor
Link to comment
Share on other sites

You defined $aFinal as a simple variable.

That's why you get the error message.

To define the dimensions of an array you have to set them when defining the array (implicitly or explicitly).

Later you can use ReDim to modify the dimensions (within limits).

The problem with your approach is that you do not know how many columns (= members of a group) you will get.

Let's say you have a group with 5 members so $aFinal will have 5 columns.

The next group has 8 members. So you need to ReDim the array to hold 8 columns.

I have no time to post a solution right now. Maybe tomorrow?

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

SUCCESS!

Thank you water. The redim command after seeing the example in the help file really made it click that I could use UBound within the subscript value.

Working code below.

#include <AD.au3>
#include <Array.au3>

_AD_Open()

Global $sGroup = "ACC_TRAVEL" ; group name
Global $aMembers, $aUserProperties, $aSplit, $aFinal

$aMembers = _AD_RecursiveGetGroupMembers($sGroup, 10, True, False) ; get group membership recursivly

For $i = 1 To UBound($aMembers) - 1
    $aUserProperties = _AD_GetObjectProperties($aMembers[$i], "displayname,description,physicalDeliveryOfficeName") ; get AD properties for each group member
    Local $sDN = "", $sDesc = "", $sDO = ""
    ; convert 2 dimensional array into a 1 dimensional array and repopulate
    For $n = 1 To $aUserProperties[0][0]
        If $aUserProperties[$n][0] = "displayname" Then
            $sDN = $aUserProperties[$n][1]
        ElseIf $aUserProperties[$n][0] = "description" Then
            $sDesc = $aUserProperties[$n][1]
        ElseIf $aUserProperties[$n][0] = "physicalDeliveryOfficeName" Then
            $sDO = $aUserProperties[$n][1]
        EndIf
    Next
    $aMembers[$i] = $aMembers[$i] & "," & $sDN & "," & $sDesc & "," & $sDO
Next
Local $aFinal[UBound($aMembers)][4] ; resize final array based on dimensions from source array
For $i = 1 To UBound($aMembers) - 1
    $aSplit = StringSplit($aMembers[$i], ",", 2) ; split values from CSV into each cell
    For $n = 0 To UBound($aSplit) - 1
        $aFinal[$i][$n] = $aSplit[$n] ; populate final array
    Next
Next
_ArrayDisplay($aFinal) ; display final array

_AD_Close()
 
Edited by kor
Link to comment
Share on other sites

BTW:

You should change line

$aMembers = _AD_RecursiveGetGroupMembers($sGroup, 10, True, False)

to

$aMembers = _AD_RecursiveGetGroupMembers($sGroup, 10, False, False)

because _AD_GetObjectProperties will not return a valid result if you pass the groupname plus the parent group name (like: groupname|name of parent group)

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

Another BTW:

You won't get valid properties "displayname,description,physicalDeliveryOfficeName" for groups contained in the group you run _AD_RecursiveGetGroupMembers for.

So you would need to check the objectclass and only run _AD_GetObjectProperties on users.

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