BillRamby
(Fresh Scripter)
2005-03-11 08:36 PM
comma delimited string or array question

I have a comma delimited string or array received from the getuserou UDF. The returned string looks like this:

OU=Users,OU=Some Site,DC=somedc,DC=somedc,DC=somedc,DC=some

What I need from the string is the second delimited value, "OU=Some Site". From there I need to strip the OU= and further break it down into "Some" and "Site" and assign each to a variable.

I've looked at the manual at ASCAN and SPLIT, but can't quite see how to make them do the job I need. Any pointers in the right direction would be appreciated.

Regards
Bill R.


Howard Bullock
(KiX Supporter)
2005-03-11 09:16 PM
Re: comma delimited string or array question

Code:

$a = "OU=Users,OU=Some Site,DC=somedc,DC=somedc,DC=somedc,DC=some"

; This works if you never include commas in any OU name.

$DNparts = split($a, ",")
? $DNparts[1]

$sitename = SubStr($DNparts[1], 4)
? $sitename

$SiteParts = split($sitename)

? $SiteParts[0]
? $SiteParts[1]

;Bring it altogether

$SiteParts2 = split(SubStr(split($a, ",")[1], 4))

? $SiteParts2[0]
? $SiteParts2[1]




Oh ya... Welcome to the board.


Les
(KiX Master)
2005-03-11 09:31 PM
Re: comma delimited string or array question

If you Split() on ',OU=' then you need not worry about commas elsewhere.

Howard Bullock
(KiX Supporter)
2005-03-11 09:34 PM
Re: comma delimited string or array question

True, but that will not automatically separate the site value from the ",DC=" domain strings. You will then need to worry about commas in the site OU name when parsing it from the DC= string.

Les
(KiX Master)
2005-03-11 09:38 PM
Re: comma delimited string or array question

Huh?

Just do a double Split(Split()) on ',DC=' first and then on ',OU='. How hard can it be?
Jooel would have no prob to do it.


Les
(KiX Master)
2005-03-11 09:42 PM
Re: comma delimited string or array question

$DN = 'OU=Users,OU=Some Site,DC=somedc,DC=somedc,DC=somedc,DC=some'
$SS = Split(Split($DN,',DC=')[0],',OU=')[1]
$SS ?


Howard Bullock
(KiX Supporter)
2005-03-11 09:49 PM
Re: comma delimited string or array question

It isn't... (PITA time) I guess that works for this particular example. There are many ways to get the goal line.

Howard Bullock
(KiX Supporter)
2005-03-11 09:52 PM
Re: comma delimited string or array question

I like it. This is a better solution as commas do not interfere.

BillRamby
(Fresh Scripter)
2005-03-14 01:24 PM
Re: comma delimited string or array question

Wow, lot of input over the weekend. I have my work cut out for me. Thanks everyone.

Regards
Bill


BillRamby
(Fresh Scripter)
2005-03-14 02:18 PM
Re: comma delimited string or array question

Ok, I want to understand this as well as use it, so I hope you don't mind some teaching.

Ok I get this part. Setting the variable
Quote:

Code:

$a = "OU=Users,OU=Some Site,DC=somedc,DC=somedc,DC=somedc,DC=some"






Ok, here you are taking the variable and making it an array (I think) of it's split parts and getting "Ou=Some Site". Does the split start at zero or one? I'm guessing 0 because the second value, which is the one needed, appears to be one here.
Quote:

Code:
 
$DNparts = split($a, ",")
? $DNparts[1]






Ok, I understand what your doing here. Your taking "OU=Some Site" and stripping off the ",OU=" as represented by the "4", as in counting from left to right ",OU=" equals 4 characters.
Quote:

Code:

$sitename = SubStr($DNparts[1], 4)
? $sitename






Ok, I got this I think. This is splitting "Some Site" into "Some" and "Site"
Quote:

Code:
 
$SiteParts = split($sitename)

? $SiteParts[0]
? $SiteParts[1]






This seems a repeat of the section above. What am I missing here?
Quote:

Code:
 
$SiteParts2 = split(SubStr(split($a, ",")[1], 4))

? $SiteParts2[0]
? $SiteParts2[1]






Oh, and one further question. Let say "OU=Some Site" actually looked like this:
OU=Region1 Houston Texas
And I would need SiteParts[0] and SiteParts[1] to be reflected this way:
SiteParts[0] would equal "Region1"
SiteParts[1] would equal "Houston Texas"
Would the space in "Houston Texas" cause problems?

Thanks again for the help.

Regards
Bill


Les
(KiX Master)
2005-03-14 02:29 PM
Re: comma delimited string or array question

yes it would.

BillRamby
(Fresh Scripter)
2005-03-14 02:32 PM
Re: comma delimited string or array question

So, would the answer be to take "$Sitename", do a substr replacing character 8 with a comma, and repeat the process?

Regards
Bill


Les
(KiX Master)
2005-03-14 02:43 PM
Re: comma delimited string or array question

First off, it bugs me that you obfuscate the facts and then come back and change the rules after code has beeen supplied to you. Why don't you spell out all the rules up front in the first place?

Second, it is Howard's code you chose to adopt and I did not bother to analyze it beyond the first weakness of the comma delimiter.

If I knew all the rules, I may answer differently. What different permutations of "some site" can there be and do you always want to peel off only the first part delimited by a space?

Without knowing all the rules... you could check UBound($SiteParts2)>1 and Join() back the second and third (or more) elements... or you could use a combination of Instr() and Substr().


BillRamby
(Fresh Scripter)
2005-03-14 02:54 PM
Re: comma delimited string or array question

No obfuscation was intended. It just didn't occure to me at the time of my first post. Different AD names in different regions. I personally work in a region that is just one word and that influenced my question. My error.

That said, some regions can have up to three words, e.g "New York City", therefore, I need to split the first section from the rest.


Les
(KiX Master)
2005-03-14 03:10 PM
Re: comma delimited string or array question

Don't mind me... sour grapes cuz you chose Howard's coode and I haven't had my 3rd cup of coffee yet.

Code:
break on
$DN = 'OU=Users,OU=Region1 New York City,DC=somedc,DC=somedc,DC=somedc,DC=some'
$SS = Split(Split($DN,',DC=')[0],',OU=')[1]
$SS ?

$SS1 = Left($SS,Instr($SS,' ')-1)
$SSLeft = Right($SS,Instr($SS,' ')+1)

'SS1 = ['+$SS1+']' ?
'SSLeft = ['+$SSLeft+']' ?



THis is still overly optimistic code. It would break if there are more OUs. For that you should check UBound().


BillRamby
(Fresh Scripter)
2005-03-14 03:20 PM
Re: comma delimited string or array question

Les, your code threw me for a moment (the ? after $ SS) but once tried it does appear to work with a lot less code. Using a combination of your and Howards code:

Code:

$OU = 'OU=Users,OU=Region2 New York City,DC=somedc,DC=somedc,DC=somedc,DC=some'
$SS = Split(Split($OU,',DC=')[0],',OU=')[1]
? $SS
$site = SubStr($SS, 9)
? $site



Returns:
Region2 New York City
New York City

Question for you. What does the apostrophe in your code do?

Whoops, just figured it out. Early morning and only two cups here so far.


Les
(KiX Master)
2005-03-14 03:28 PM
Re: comma delimited string or array question

Still the sour grapes...

What is wrong with using my code?

The part SubStr($SS, 9) will break if you go to Region10 or more!


BillRamby
(Fresh Scripter)
2005-03-14 03:32 PM
Re: comma delimited string or array question

Nothing wrong with the code. I just didn't see the value expressed when I ran it in a window until I changed

$SS ?
to
? $SS

Actually I'm checking your new code now. I get the results of:
SS1 = [Region1]
SSLeft = [York City]

Working on where "New" went now.


Les
(KiX Master)
2005-03-14 03:35 PM
Re: comma delimited string or array question

DOH!
I should test my code before posting it.


Les
(KiX Master)
2005-03-14 03:41 PM
Re: comma delimited string or array question

Dang... had to RTFM.

$SSLeft = Right($SS,Len($SS)-Instr($SS,' '))


BillRamby
(Fresh Scripter)
2005-03-14 03:55 PM
Re: comma delimited string or array question

Works. Now a question so I understand.

How did getting the length of $SS and subtracting the space make the difference? Or did I interpret what you did wrong?

Regards
Bill


Les
(KiX Master)
2005-03-14 03:59 PM
Re: comma delimited string or array question

You interpreted wrong.

Instr() returns the position of the first space it finds. Len()-Instr() determines how much of the Right() side of the string to return.


BillRamby
(Fresh Scripter)
2005-03-14 04:17 PM
Re: comma delimited string or array question

Ahhh. Ok, I wasn't reading the formula right. Thx.

So in the end, the whole script looks like this.
Code:

$ou = GetUserOU()
$SS = Split(Split($ou,',DC=')[0],',OU=')[1]
$SS1 = Left($SS,Instr($SS,' ')-1)
$SSLeft = Right($SS,Len($SS)-Instr($SS,' '))
SET "SITE="+$SSLeft
SET "REGION="+$SS1

Function GetUserOU()
Redim $TempArray[0]
Redim $GetUserOU
$adSys = CreateObject("ADSystemInfo")
$TempArray = Split($adSys.UserName,",")
If Right($TempArray[0],1) = "\"
$Start = 2
Else
$Start = 1
EndIf

For $Counter = $Start To UBound($TempArray)
If Len($GetUserOU) = 0
$GetUserOu = $TempArray[$Counter]
Else
$GetUserOu = $GetUserOu + "," + $TempArray[$Counter]

EndIf
Next

$adSys = 0
EndFunction GetUserOU()



Cool, and many thanks. Just made my job as a Citrix Admin a whole lot easier.

hhmm, question about codiquette. Was it ok to remove the remarked out data of the getuserou UDF as supplied, or is it more proper to leave it in?


Les
(KiX Master)
2005-03-14 04:22 PM
Re: comma delimited string or array question

Etiquette dictates that 2 lines of comment should be included. ! line with the author, and 1 line with the URL.

BillRamby
(Fresh Scripter)
2005-03-14 04:50 PM
Re: comma delimited string or array question

Actually, I decided to leave more than that and add to it. I just know someone, somehow, sometime, is going to make me revisit this in the future.

Code:

; Get users AD OU data by calling the getuserou function
$ou = GetUserOU()
; Take the string and split out the needed part
$SS = Split(Split($ou,',DC=')[0],',OU=')[1]
; Take the resulting string and parse left from the first space
$SS1 = Left($SS,Instr($SS,' ')-1)
;Take the string and parse right from the first space
$SSLeft = Right($SS,Len($SS)-Instr($SS,' '))
; Set the system variables
SET "SITE="+$SSLeft
SET "REGION="+$SS1

;Function GetUserOU()
;
;Author Remko Weijnen
;
;Action Returns the DN of the OU/Container which the user is placed in
;
;http://www.kixhelp.com/udfs/udf/112015.htm
;
;Returns If the DN eq CN=SomeUser,OU=SomeOU,DC=ad,DC=local
; it returns OU=SomeOU,dc=ad,dc=local
;
;
Function GetUserOU()
Redim $TempArray[0]
Redim $GetUserOU
$adSys = CreateObject("ADSystemInfo")
$TempArray = Split($adSys.UserName,",")
; Check if there's a comma in the CN, eg if the user name = "Surname, Name" AD returns "Surname\, Name"
If Right($TempArray[0],1) = "\"
$Start = 2
Else
$Start = 1
EndIf

For $Counter = $Start To UBound($TempArray)
If Len($GetUserOU) = 0
$GetUserOu = $TempArray[$Counter]
Else
$GetUserOu = $GetUserOu + "," + $TempArray[$Counter]

EndIf
Next

$adSys = 0
EndFunction GetUserOU()