#116427 - 2004-03-19 03:14 PM
method to break out of for each loop
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
I have this piece of code running from EnumOUs(): Code:
.... $i='' $objOU = GetObject($LDAP) $aFilter[0] = $Filter $objOU.Filter = $aFilter for each $item in $objOU if $item.class = $Filter $i = $LDAP endif next ....
I would like to break out of the FOR EACH as soon as $i gets a value, but stay in the function.
|
|
Top
|
|
|
|
#116429 - 2004-03-19 03:27 PM
Re: method to break out of for each loop
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
hahahahhahaha
It has been so long since I used a goto, I forgot about it
|
|
Top
|
|
|
|
#116430 - 2004-03-19 03:30 PM
Re: method to break out of for each loop
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Hmm. Not tested, but (with novarsinstrings) maybe... Code:
$i='' $objOU = GetObject($LDAP) $aFilter[0] = $Filter $objOU.Filter = $aFilter $result=Execute('for each $item in $objOU if $item.class = $Filter $i = $LDAP if $i="some value" Exit 1 EndIf endif next Exit 0')
As a benefit, $result will be true (1) if the result is found.
|
|
Top
|
|
|
|
#116432 - 2004-03-19 03:38 PM
Re: method to break out of for each loop
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
so GOTO (ergg) is definetly the only way
No, an "Exit" from an "Executed" branch will only exit the "Executed" code, not the function.
Not a pretty solution, but at least I haven't soiled the code with the infamous G word.
|
|
Top
|
|
|
|
#116434 - 2004-03-19 03:47 PM
Re: method to break out of for each loop
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
the issue is pertaining to the amount of time spent in looping in the code.
It's purpose if to return all the OUs that contain either user or computer objects.
Once it finds one, there is no longer any reason to keep foreaching.
by adding the goto, the runtime on my network goes from about 20 seconds to 4 seconds.
This will make McAfeeManager and some of the other kforms apps that use EnumOUs() much faster.
Code:
Function EnumOUs($LDAP, $Filter) dim $aFilter[0], $pos, $objOU, $i, $j
if $Filter <> 'user' $Filter = 'computer' endif $objOU = GetObject($LDAP) if not VarTypeName($objOU)='Object' exit 1 endif
$aFilter[0] = $Filter $objOU.Filter = $aFilter for each $item in $objOU $PBar1.Value = iif($PBar1.Value < $PBar1.Max,$PBar1.Value + 1,0) ;for progress bar only if $item.class = $Filter $i = $LDAP goto jump endif next :jump $aFilter[0] = "organizationalUnit" $objOU.Filter = $aFilter for each $item in $objOU $PBar1.Value = iif($PBar1.Value < $PBar1.Max,$PBar1.Value + 1,0) ;for progress bar only $Name = $item.Name $pos = instrrev($LDAP,"/") $DN = Left($LDAP,$pos) + $Name + ", " + substr($LDAP, $pos+1) $j = EnumOUs($DN, $Filter) if $j $i = iif($i,$i +"|"+ $j,$j) endif next $EnumOUs = $i Endfunction
|
|
Top
|
|
|
|
#116435 - 2004-03-19 03:58 PM
Re: method to break out of for each loop
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Well, the Execute / Exit will do exactly the same.
However if you're more comfortable with the G thing then use that, it's just as valid and easier to code.
Just don't over do it or the Code Conformist Cops might drag you in for re-education
|
|
Top
|
|
|
|
#116437 - 2004-03-19 05:27 PM
Re: method to break out of for each loop
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Quote:
ExitFor gets my vote
Maybe ElseIF too?
Kent
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 2220 anonymous users online.
|
|
|