Jump to content
Sign in to follow this  
Ostap

StringSplit returns empty aray

Recommended Posts

Hi, 

I need to parse a text file extracted from active directory, and string looks like this: 

CN=Firstname0\, Lastname0 (UserID0),OU=_Marked for deletion,DC=Fabrikam,DC=COM^CN=Firstname1\, Lastname0 (UserID0),OU=Users,OU=EUR,DC=Fabrikam,DC=COM^CN=Firstname2\, Lastname2 (UserID3),OU=Users,OU=EUR,DC=Fabrikam,DC=COM

Code is very basic

$String0 = "CN=Firstname0\, Lastname0 (UserID0),OU=_Marked for deletion,DC=Fabrikam,DC=COM^CN=Firstname1\, Lastname0 (UserID0),OU=Users,OU=EUR,DC=Fabrikam,DC=COM^CN=Firstname2\, Lastname2 (UserID3),OU=Users,OU=EUR,DC=Fabrikam,DC=COM"
MsgBox (0, "$String0", $String0 ) ; returns msgbox with above string
$Members = StringSplit ( $String0, "^" )
MsgBox (0, "$Members", $Members ) ; returns empty

StringSplit returns empty array no matter what delimiters I try. What I am doing wrong? 

Share this post


Link to post
Share on other sites

In case of an error each AutoIt function either sets the return value or macro @error to a value denoting the kind of problem.
It is good practice to check this values after each function call.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-09-05 - Version 1.5.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-27 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

@Ostap welcome to the forum. As mentioned above, the problem is a combination of trying to use the wrong tool for the job (a MsgBox to display an array) and a lack of error checking. I would suggest in addition that you spend some time reading the help file page for the particular function you plan to use. The examples provided (in this case 3) will show you the proper way to utilize the command to retrieve the results you're after.


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites

I do not know which tool you run to extract the data from AD, but AutoIt provides the AD UDF to directly access AD and retrieve the needed information for further processing.
For download please check my signature.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-09-05 - Version 1.5.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-27 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites

_IsArray doesn't work for StringSplit as this function always returns an array.
If the delimiter isn't found then @error is set to 1 and the full string is returned in $aArray[0] or $aArray[1] (depending on the $STR_NOCOUNT flag.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2020-09-05 - Version 1.5.1.1) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2020-06-27 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (NEW 2020-06-27 - Version 1.3.2.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - 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
Task Scheduler (2019-12-03 - Version 1.5.1.0) - Download - General Help & Support - Wiki

Tutorials:
ADO - Wiki, WebDriver - Wiki

 

Share this post


Link to post
Share on other sites
#include <MsgBoxConstants.au3>
#include <StringConstants.au3>
#include <Array.au3>

$String0 = ("CN=Firstname0\, Lastname0 (UserID0),OU=_Marked for deletion,DC=Fabrikam,DC=COM" _
         & "^CN=Firstname1\,Lastname0 (UserID0),OU=Users,OU=EUR,DC=Fabrikam,DC=COM" _
         & "^CN=Firstname2\, Lastname2 (UserID3),OU=Users,OU=EUR,DC=Fabrikam,DC=COM")
$Members = StringSplit ( $String0, "^",2)

Dim $Arr[0]

For $x = 0 to UBound($Members)-1
    ConsoleWrite($Members[$x]&@CRLF)
    _ArrayAdd($Arr,$Members[$x])
Next

_ArrayDisplay($Arr)

 

Share this post


Link to post
Share on other sites

@rootx

So, you create an array with StringSplit, then cycle through that array to create a second array, then display the second array (which is exactly the same as the $Members array originally returned). Rube Goldberg much?


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites

!!! :P

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

$String0 = ("CN=Firstname0\, Lastname0 (UserID0),OU=_Marked for deletion,DC=Fabrikam,DC=COM" _
         & "^CN=Firstname1\,Lastname0 (UserID0),OU=Users,OU=EUR,DC=Fabrikam,DC=COM" _
         & "^CN=Firstname2\, Lastname2 (UserID3),OU=Users,OU=EUR,DC=Fabrikam,DC=COM")
$Members = StringSplit ( $String0, "^",2)

_ArrayDisplay($Members)

For $x = 0 to UBound($Members)-1
    ConsoleWrite($Members[$x]&@CRLF)
Next

 

Share this post


Link to post
Share on other sites

Just the same @rootx, both _ArrayDisplay and ConsoleWrite are for debugging purposes only. Why double the script's work by using both?


"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites

you can always put some debug in your debug :)

#include <Array.au3>

$String0 = ("CN=Firstname0\, Lastname0 (UserID0),OU=_Marked for deletion,DC=Fabrikam,DC=COM" _
         & "^CN=Firstname1\,Lastname0 (UserID0),OU=Users,OU=EUR,DC=Fabrikam,DC=COM" _
         & "^CN=Firstname2\, Lastname2 (UserID3),OU=Users,OU=EUR,DC=Fabrikam,DC=COM")

$members = StringSplit ( $String0, "^",2)
_ArrayDisplay($members , "Array+Console" , "" , 0 , 0 , 0 , 0 , 0 , consolewrite(_ArrayToString($members , @CR)))

 


,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...