Page 1 of 1 1
Topic Options
#116427 - 2004-03-19 03:14 PM method to break out of for each loop
Radimus Moderator Offline
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.
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#116428 - 2004-03-19 03:20 PM Re: method to break out of for each loop
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Do you mean without?

SetOption('FlameShield','on')
GOTO $Skip
SetOption('FlameShield','off')

This has been discussed several times with regard to suggestions for an EXIT FOR feature.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#116429 - 2004-03-19 03:27 PM Re: method to break out of for each loop
Radimus Moderator Offline
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
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#116430 - 2004-03-19 03:30 PM Re: method to break out of for each loop
Richard H. Administrator Offline
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
#116431 - 2004-03-19 03:34 PM Re: method to break out of for each loop
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
hmmm ...

Quote:

I would like to break out of the FOR EACH as soon as $i gets a value, but stay in the function.






so GOTO (ergg) is definetly the only way ...

[edit: ooops didn't see the execute , might work !]


Edited by Jochen (2004-03-19 03:36 PM)
_________________________



Top
#116432 - 2004-03-19 03:38 PM Re: method to break out of for each loop
Richard H. Administrator Offline
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
#116433 - 2004-03-19 03:42 PM Re: method to break out of for each loop
Jochen Administrator Offline
KiX Supporter
*****

Registered: 2000-03-17
Posts: 6380
Loc: Stuttgart, Germany
Perfect !!!

Proof :

Code:
break on


$array = 0,0,1,0,0

$_ = function1($array)

get $
exit 1

function function1($a)
$_ = execute("for each $$e in $$a
if $$e = 1
'element = 1, exiting' ? exit 1
else
'$$e' ?
endif
next
")
"still in function" ?
endfunction




Well, still an ExitFor gets my vote


Edited by Jochen (2004-03-19 03:43 PM)
_________________________



Top
#116434 - 2004-03-19 03:47 PM Re: method to break out of for each loop
Radimus Moderator Offline
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

_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#116435 - 2004-03-19 03:58 PM Re: method to break out of for each loop
Richard H. Administrator Offline
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
#116436 - 2004-03-19 04:09 PM Re: method to break out of for each loop
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
That's what SetOption('FlameShield','on') is for.

Have you tested the Execute() method for memory leaks? Ruud does do stack cleanup for GOTOs.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#116437 - 2004-03-19 05:27 PM Re: method to break out of for each loop
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Quote:

ExitFor gets my vote




Maybe ElseIF too?

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 2220 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.072 seconds in which 0.034 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org