#119993 - 2004-05-24 01:16 AM
Structured EnumKey Loop /w a single EnumKey
|
Shawn
Administrator
   
Registered: 1999-08-13
Posts: 8611
|
Someone on korg wrote a non-goto EnumKey loop a while back, that only uses a single EnumKey call, anyone care to give a try. The manual example uses gotos:
Code:
$Index = 0
:Loop1
$KeyName = ENUMKEY("HKEY_CURRENT_USER\Console\", $Index)
If @ERROR = 0
? "Name found: $KeyName"
$Index = $Index + 1
goto Loop1
Endif
I know of a structured way using two EnumKeys:
Code:
$i = 0
$KeyName = EnumKey("HKEY_CURRENT_USER\Software\",$i)
While $KeyName
?"Name found: $KeyName"
$i=$i+1
$KeyName = EnumKey("HKEY_CURRENT_USER\Software\",$i)
Loop
But someone wrote a structured way using a single EnumKey ? Anyone ?
|
|
Top
|
|
|
|
#119994 - 2004-05-24 01:44 AM
Re: Structured EnumKey Loop /w a single EnumKey
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
Does this work?
Code:
$i = 0 Do $KeyName = EnumKey("HKEY_CURRENT_USER\Software\",$i) "Name found: $KeyName" ? $i=$i+1 Until Not $KeyName
|
|
Top
|
|
|
|
#120000 - 2004-05-24 10:51 AM
Re: Structured EnumKey Loop /w a single EnumKey
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
A bit over-the-top, but this works: Code:
$i = 0 While Execute('$$Key=EnumKey("HKEY_CURRENT_USER\Software\",$$i) Exit Not @@ERROR') "Name found: "+$Key ? $i=$i+1 Loop
|
|
Top
|
|
|
|
#120001 - 2004-05-24 04:06 PM
Re: Structured EnumKey Loop /w a single EnumKey
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Shawn,
I've been racking my brain and I can't think why you want to do this unless it is purely for aesthetic reasons. It's not like having the EnumKey in twice is a processing overhead.
In fact the solution I gave above is worse in terms of resources, as the Execute()'ed statement has to be parsed on each loop.
It does have a better Golf score however
|
|
Top
|
|
|
|
#120005 - 2004-05-24 04:39 PM
Re: Structured EnumKey Loop /w a single EnumKey
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Be careful when using @ERROR at the start of loops if you don't precede it with the function.
Take this for example, exactly the same procedure run twice:
Code:
$Index = 0
While @Error=0
$KeyName=ENUMKEY("HKEY_CURRENT_USER\Console\", $Index)
If @ERROR = 0 ?
"Name found on first pass: $KeyName"
$Index=$Index+1
EndIf
Loop
$Index = 0
While @Error=0
$KeyName=ENUMKEY("HKEY_CURRENT_USER\Console\", $Index)
If @ERROR = 0 ?
"Name found on second pass: $KeyName"
$Index=$Index+1
EndIf
Loop
The problem is that the second pass will not display any results.
The reason is that @ERROR from the first loop is still set, so that the second loop fails instantly.
In a more complex function this can be a bugger to spot.
The solution is to clear the error status before starting the loop:
Code:
$Index=Execute("Exit 0") ; Initialise index and clear error status
While @Error=0
$KeyName=ENUMKEY("HKEY_CURRENT_USER\Console\", $Index)
If @ERROR = 0 ?
"Name found on first pass: $KeyName"
$Index=$Index+1
EndIf
Loop
$Index=Execute("Exit 0") ; Initialise index and clear error status
While @Error=0
$KeyName=ENUMKEY("HKEY_CURRENT_USER\Console\", $Index)
If @ERROR = 0 ?
"Name found on second pass: $KeyName"
$Index=$Index+1
EndIf
Loop
[EDIT]
Spooky. I'll have to type quicker.
[/EDIT]
Edited by Richard H. (2004-05-24 04:42 PM)
|
|
Top
|
|
|
|
#120007 - 2004-05-25 05:29 AM
Re: Structured EnumKey Loop /w a single EnumKey
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
I go back to my original sample with a little tweak...
Code:
$i = 0 Do $KeyName = EnumKey("HKEY_CURRENT_USER\Software\",$i) If Not @ERROR "Name found: $KeyName" ? $i=$i+1 EndIf Until @ERROR
...or, if you'd rather... Code:
$i = 0 Do $KeyName = EnumKey("HKEY_CURRENT_USER\Software\",$i) If $KeyName "Name found: $KeyName" ? $i=$i+1 EndIf Until Not $KeyName
|
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 837 anonymous users online.
|
|
|