Page 1 of 1 1
Topic Options
#119993 - 2004-05-24 01:16 AM Structured EnumKey Loop /w a single EnumKey
Shawn Administrator Offline
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. Offline
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
#119995 - 2004-05-24 02:09 AM Re: Structured EnumKey Loop /w a single EnumKey
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
well, close but no cigar ... if the key really doesn't have any subkeys, it still prints a null subkey plus, it prints a null subkey at the end ... need something eloquent like this, but covers all the bases too.
Top
#119996 - 2004-05-24 04:05 AM Re: Structured EnumKey Loop /w a single EnumKey
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Code:
Break on
$Index = 0
While @Error=0
$KeyName = ENUMKEY("HKEY_CURRENT_USER\Console\", $Index)
If @ERROR = 0
? "Name found: $KeyName"
$Index = $Index + 1
Endif
Loop

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#119997 - 2004-05-24 05:00 AM Re: Structured EnumKey Loop /w a single EnumKey
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
niiiice ...
Top
#119998 - 2004-05-24 06:24 AM Re: Structured EnumKey Loop /w a single EnumKey
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
See also the ArrayEnumKey() UDF in the UDF Forum.

You cannot use a single EnumKey() function to enumerate multiple keys, unless you use the EnumKey() function inside a loop.
_________________________
There are two types of vessels, submarines and targets.

Top
#119999 - 2004-05-24 07:30 AM Re: Structured EnumKey Loop /w a single EnumKey
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
why not:
Code:

for $i=0 to -1
$keyname = ENUMKEY("HKEY_CURRENT_USER\Console\", $i)
if @error
$i=-1
else
? "Name found: " $keyname
endif
Loop

_________________________
!

download KiXnet

Top
#120000 - 2004-05-24 10:51 AM Re: Structured EnumKey Loop /w a single EnumKey
Richard H. Administrator Offline
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 Offline
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
#120002 - 2004-05-24 04:15 PM Re: Structured EnumKey Loop /w a single EnumKey
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
It was purely for aesthetic reasons and just on principle. Everytime I had to write an enum loop I always ended-up doing the double enumkey version, always knowing in the back of my mind that this single enumkey version was out there and just seemed more eloquent thats all. I think the single enumkey version is less kludgey and less (down-the-road-changes) error-prone.

-Shawn

Top
#120003 - 2004-05-24 04:23 PM Re: Structured EnumKey Loop /w a single EnumKey
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I imagine it is also a case of how to avoid GOTOs in the simplest manner. The problem with my method of LOOPing on @Error is that in the real world, @Error may happen at unexpected times and for unexpected reasons. Take MessageBox(), for instance. The post over at
http://www.scriptlogic.com/forums/display_message.asp?mid=8877 demonstrates how a successful MessageBox() throws @Error.

Code:

$rc = Messagebox("Hello World","testing...")
$rc = Messagebox("@@Error = "+@error,"testing...")



BTW, I think that Ruud should fix that.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#120004 - 2004-05-24 04:28 PM Re: Structured EnumKey Loop /w a single EnumKey
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Les - ja - thought same thing when I first read your code - it starts with an "open-ended" check of @ERROR which might cause trouble. Once the loop gets going though, should be perfectly fine.
Top
#120005 - 2004-05-24 04:39 PM Re: Structured EnumKey Loop /w a single EnumKey
Richard H. Administrator Offline
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
#120006 - 2004-05-24 04:45 PM Re: Structured EnumKey Loop /w a single EnumKey
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Sorry, just rising to the challenge of your limited scope. You never said it had to be bulletproof.

Keeping with the @Error method, someone wrote a one line UDF that clears @Error with an EXIT 0. Useful for times like this.

This works too but processes an IF on every LOOP.
Code:
Break on
$i = 0
$KeyName = 'dummy'
While $KeyName
If $i
?"Name found: $KeyName"
EndIf
$i=$i+1
$KeyName = EnumKey("HKEY_CURRENT_USER\Software\",$i)
Loop

_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#120007 - 2004-05-25 05:29 AM Re: Structured EnumKey Loop /w a single EnumKey
Chris S. Offline
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
Page 1 of 1 1


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

Who's Online
0 registered and 837 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.065 seconds in which 0.023 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