NTDOCAdministrator
(KiX Master)
2006-09-21 10:14 PM
KiXtart Learning Series - Lesson 01

In response to this post I've come up with a very mini challenge
OT (off)/ OT (on) : Golfing / Minigolf
http://www.kixtart.org/ubbthreads/showflat.php?Cat=0&Number=168388

    RULES
  • No KiXtart.org Moderators or Administrators aside from myself are allowed to post in this thread.
  • (This is for those with less experience to learn, not for bored, experienced users to bump their post count)
  • (If you're able to easily do this task then please don't participate with any solutions)
  • (If the task is too easy then I'll increase the difficulty level next time)
  • No using IM,PM or Google to locate/find an answer (you're on the honor system here).


Objective: Check if registry key contains an expected value of "Enterprise Terminal Server"

Using REGEDIT.EXE create the key Golfing under HKEY_CURRENT_USER\Software
Then under the new Golfing key create a new Multi-String Value named Round1 and
enter the following value.
Enterprise {press enter key}
Terminal {press enter key}
Server {press enter key}

Now using KiXtart check if that value exists or not.

DO NOT Post any solution for this though until Friday, Sep 22


NTDOCAdministrator
(KiX Master)
2006-09-22 12:57 AM
Re: Mini-Golf Round 01

For those that want an easy method to create the key I've provided
a .REG file you can download and run to create the key for this objective.

Download golfing.reg


Benny69
(MM club member)
2006-09-22 02:18 AM
Re: Mini-Golf Round 01

Doc,
Could you clarify, do you want this as a code snippet or a function?


NTDOCAdministrator
(KiX Master)
2006-09-22 02:31 AM
Re: Mini-Golf Round 01

Just a script method is fine. Does not need to be a UDF.

Want to see if the user knows how to do it.

I agree with Gargoyle that it might be nice to at least briefly explain what your code is doing or what you "think" your code is doing so that if needed it can be critiqued.

I'm in no way wanting to slam anyone for their code or explanation. Just want to help others to get better at scripting overall.


Björn
(Korg Regular)
2006-09-22 10:16 AM
Re: Mini-Golf Round 01

When are we ok'ed to post timewise? it's friday, and I think I've got it

PaulyT
(Fresh Scripter)
2006-09-22 05:03 PM
Re: Mini-Golf Round 01

I am ready too! When please? Ready set go...

Gargoyle
(MM club member)
2006-09-22 05:09 PM
Re: Mini-Golf Round 01

Mine is ready as well...

NTDOCAdministrator
(KiX Master)
2006-09-22 07:00 PM
Re: Mini-Golf Round 01

Please post your solutions now. Thanks.

I may be away from the computer for a bit but will respond soon.


Gargoyle
(MM club member)
2006-09-22 07:12 PM
Re: Mini-Golf Round 01

Code:

Dim $SO,$Re ,$R, $S, $J

;Script Options
$SO=SETOPTION("Explicit", "ON")
$SO=SETOPTION("NoMacrosInStrings", "ON")
$SO=SETOPTION("NoVarsInStrings", "ON")
$SO=SETOPTION("WrapAtEOL", "ON")
BREAK ON

;Read the value at the given location
$R = ReadValue("HKCU\Software\Golfing","Round1")

;Split the value as a Reg_Multi will include the | character
$S = Split($R,"|")

;Now take each of the elements and put them back together
;with a space to check the value = what we want

$J = Join($S," ")

;Does it match?
If $J = "Enterprise Terminal Server ";space added at end as the join adds one
    ? "We have a match. The string returned was: " $J
Else
    ? "No match found. The string returned was: " $J
EndIf

;The value of $J was verified by printing out each of the elements of
;$S Array with the following code

/*

For $ = 0 to Ubound($S)
?
$S[$]
Next

*/

;When attempting to do the join, with the optional "count", it did not work.
;I was unable to determine why the following would not work

/*
$J = Join($S," ",Ubound($s)-1)
*/



;To make this more robust one should verify that the value exist first
;then they should read the value type and write a routine to know how the
;expected return is going to be formatted, by no means should the following
;snippet be deemed fully functional, one would need to evaluate the returns
;of the different data types and build the appropriate "CASE" statement for
;that scenario

;Example

/*

If KeyExist("HKCU\Software\Golfing\Round1") = 1
   
$Type = Readtype("HKCU\Software\Golfing","Round1")
   
$R = ReadValue("HKCU\Software\Golfing","Round1")
   
   
Select
        Case $Type = "Reg_Multi_SZ"
            $S = Split($R,"|")
           
$J = Join($S," ")
       
Case $Type = "Reg_SZ"
            $J = $S
        Case 1
           
@Error = 5
   
EndSelect

Else
    Exit 1
EndIf

*/



PaulyT
(Fresh Scripter)
2006-09-22 07:39 PM
Re: Mini-Golf Round 01

I had two solutions.

Solution 1
Code:

$ = ReadValue('HKCU\Software\Golfing', 'Round1')
Do
$ = SubStr($, 1, InStr($, '|') - 1) + ' ' + SubStr($, InStr($, '|') + 1, Len($))
Until InStr($, '|') = 0
If Rtrim($) = 'Enterprise Terminal Server'
? 'Value Present'
EndIf



Solution 2
Code:
  
$ = Split(ReadValue('HKCU\Software\Golfing', 'Round1'), '|')
For Each $a in $
If InStr('Enterprise Terminal Server', $a) = 0
Exit
EndIf
Next
? 'Value Present'



NTDOCAdministrator
(KiX Master)
2006-09-22 10:01 PM
Re: Mini-Golf Round 01

Good Gargoyle... top solution works as expected.

Your bottom posted code though does not return a value for me, please review your code so that it provides the expected results.


NTDOCAdministrator
(KiX Master)
2006-09-22 10:02 PM
Re: Mini-Golf Round 01

Thanks for the solutions Paul, both work as expected.

Though upon reviewing your code I'm not so sure I'd place you in the noobie category.
That's okay though as we progress the task will become more difficult.

I'll post another solution once Bjorn is done unless someone else posts similar code.



NTDOCAdministrator
(KiX Master)
2006-09-22 10:06 PM
Re: Mini-Golf Round 01

Okay, waiting for Bjorn to post his.

NTDOCAdministrator
(KiX Master)
2006-09-22 10:11 PM
Re: Mini-Golf Round 01

Further reading to help learn more about the Registry

Registry
http://www.kixtart.org/ubbthreads/showflat.php?Cat=&Number=81601


Björn
(Korg Regular)
2006-09-22 10:51 PM
Re: Mini-Golf Round 01

Well, took some time to get to work again, but here it is, one warning - it's long

Code:

/* Minigolfing 01*/

If NOT @LOGONMODE
Break On
EndIf

Dim $SO,$Key,$wr
$SO = SetOption("Explicit","On")
$SO = SetOption("NoVarsInStrings","On")
$SO = SetOption("NoMacrosInStrings","On")

$key='HKEY_CURRENT_USER\Software\','Golfing','Round1','Enterprise Terminal Server'
;The first two is the key, third is the value, and forth the data we are looking for.

$wr=FindMe($key) ; will return a code to @error: 0 for true, and 1 for not true
@error

Function FindMe($key)
Dim $ch,$found,$index,$valuename,$KeyF,$JoinKey,$Valuename2,$wr,$split,$inst
$ch=KeyExist($key[0]) ; Does the HKEY_CURRENT_USER_\Software\ exist?
if $ch ;If it does....
$found=0
$index=0 ;Setting values so the while-statement will not run forever..

while @error=0 and $found=0 ;While no errors and $found is still 0
$ValueName = ENUMKEY($key[0],$index) ;Lists the subkey, $index points to the 'number' in the list
if $ValueName = $Key[1] $found=1 ;If the subkey is the same as the $Key[1] (second val in array), $found is set to 1
$KeyF=$key[0],$ValueName ;make an array out of $Key[0] and $ValueName
$JoinKey=Join($KeyF,'') ;Join 'em together
$wr=ReadValue($JoinKey,$Key[2]) ;Reads the data of value in the key
$split=Split($wr,'|') ;Current format is seperated by '|', therefore remove those
$wr=Join($split,' ') ; and join 'em together
$inst=Instr($wr,$Key[3]) ;Does $wr contain the same as $Key[3] ?
if $inst exit 0 else exit 1 endif ; If it does, exit with a 0, otherwise exit with a 1
else
$Index = $Index + 1 ;$ValueName wasn't the same as $Key[1], continues to look for matches.
endif
loop

exit @error ; In case of an unexpected error comes along, just exit with it (dunno if really needed)

endif

EndFunction



Gargoyle
(MM club member)
2006-09-22 10:53 PM
Re: Mini-Golf Round 01

My second piece was not meant to be functional and was never tested. However at your request...

Code:

Dim $Type, $S, $J, $R

If KeyExist("HKCU\Software\Golfing") = 1
   
$Type = Readtype("HKCU\Software\Golfing","Round1")
   
$R = ReadValue("HKCU\Software\Golfing","Round1")
   
   
Select
        Case $Type = "Reg_Multi_SZ"
            $S = Split($R,"|")
           
$J = Join($S," ")
       
Case $Type = "Reg_SZ"
            $J = $R
        Case 1
           
@Error = 5
   
EndSelect
If
$J = "Enterprise Terminal Server ";space added at end as the join adds one
    ? "We have a match. The string returned was: " $J
Else
    ? "No match found. The string returned was: " $J
EndIf


Else
    Exit 1
EndIf



Björn
(Korg Regular)
2006-09-22 10:54 PM
Re: Mini-Golf Round 01

Nice going Garg! You acctually added what I was thinking about - what kind of entry it was! I see some similarities in our code

Gargoyle
(MM club member)
2006-09-22 10:55 PM
Re: Mini-Golf Round 01

To much time around the likes of Doc and Jooel can do that to you.

NTDOCAdministrator
(KiX Master)
2006-09-22 10:58 PM
Re: Mini-Golf Round 01

Well I must say quite an innovative method, did not expect a UDF as such.

The code appears to be operational though.

I have to run for a bit, but will come back and post another solution as well as start some discussion on pros/cons of methods used.


Björn
(Korg Regular)
2006-09-22 11:02 PM
Re: Mini-Golf Round 01

Garg - you've got a message!
Doc, well.. urhnm, you know how my 'coding' is


PaulyT
(Fresh Scripter)
2006-09-22 11:19 PM
Re: Mini-Golf Round 01

Appriciate the feed back. I certainly hurt my brain looking at the regular Kix Golf sometimes and the solutions that are created.

Björn
(Korg Regular)
2006-09-22 11:27 PM
Re: Mini-Golf Round 01

You're kidding... Your snippets did the same thing as ours, and how many lines did you use?

NTDOCAdministrator
(KiX Master)
2006-09-22 11:34 PM
Re: Mini-Golf Round 01

Okay here is another solution as well.

Then let's discuss some of the pros/cons of the coding everyone has posted.


Dim $Key, $Value
$Key = 'HKEY_CURRENT_USER\Software\Golfing'
$Value = ReadValue($Key,'Round1')
If InStr($Value,'Enterprise|Terminal|Server|')
'Found value' ?
Else
'Value not found' ?
EndIf


Gargoyle
(MM club member)
2006-09-22 11:41 PM
Re: Mini-Golf Round 01

My first thought is that we all overthought the whole process. In my case I kept thinking about how to ensure that all possible errors were handled.

Björn
(Korg Regular)
2006-09-22 11:42 PM
Re: Mini-Golf Round 01

Well - My version looks like a novell, but being a function makes it a bit usefull.. BUT, not happy about how it goes thro the reg. Your versions doesn't lack error-checking tho - so I see no point in using a novell over a fewer lines of code...

NTDOCAdministrator
(KiX Master)
2006-09-23 12:28 AM
Re: KiXtart Learning Series - Round 01

Comments on Gargoyle's code



Dim $SO,$Re ,$R, $S, $J
; Dimming variables is a good practice
; Potential issues:
; A.) No issues in small one off scripts
; B.) In a large script single letter variables can be difficult to manage

$SO=SETOPTION("Explicit", "ON")
$SO=SETOPTION("NoMacrosInStrings", "ON")
$SO=SETOPTION("NoVarsInStrings", "ON")
$SO=SETOPTION("WrapAtEOL", "ON")
BREAK ON
; Setting options is a good practice
; Most admins would rather not have BREAK ON for a Logon script, using
If Not @LogonMode
Break On
EndIf
; As recently posted, used by Witto would appear to be a good practice as well


; Semi personal preference but single quotes ' would go within double quotes "
; My own preference has been to now use the single for most scripting
; However again this is not necessarilly a Best Practice more so than a preference
; Knowing that single within double is the key point to remeber when coding more
; complex scripts where needed.

$R = ReadValue("HKCU\Software\Golfing","Round1")

$S = Split($R,"|")

$J = Join($S," ")

; Using CHR(20) or similar as needed is another option/choice when dealing with complex scripting


NTDOCAdministrator
(KiX Master)
2006-09-23 12:35 AM
Re: KiXtart Learning Series - Round 01

Comments on Paul's code


; No vars declared, not a best practice for production scripting 
; Use of single character vars can be difficult to manage in large scripts
; and scope issues could easily creep in.

$ = ReadValue('HKCU\Software\Golfing', 'Round1')

; Nice use of InStr and SubStr for others to see examples of possible methods
; of locating specific data
Do
$ = SubStr($, 1, InStr($, '|') - 1) + ' ' + SubStr($, InStr($, '|') + 1, Len($))
Until InStr($, '|') = 0
If Rtrim($) = 'Enterprise Terminal Server'
? 'Value Present'
EndIf


NTDOCAdministrator
(KiX Master)
2006-09-23 01:02 AM
Re: KiXtart Learning Series - Round 01

Comments on Bjorn's code



If NOT @LOGONMODE
Break On
EndIf

Dim $SO,$Key,$wr
$SO = SetOption("Explicit","On")
$SO = SetOption("NoVarsInStrings","On")
$SO = SetOption("NoMacrosInStrings","On")

; Good practice setting the options, including the break on method.


; Minor observation so that other new scripters are aware
; KiXtart can use abbeviations for some of the main Registry keys


;Registry Functions
;All registry functions use the following format to specify registry subkeys:
;[\\remote_computer_name\][Key\]Subkey
;Remote_computer_name can be any valid computer name in UNC format (preceded by two backslashes).
;If you do not specify a remote_computer_name, the program defaults to the local registry.
;Key can be any of the main registry trees: HKEY_LOCAL_MACHINE (or HKLM in short),
;HKEY_USERS (HKU), HKEY_CLASSES_ROOT (HKCR), HKEY_CURRENT_USER (HKCU) or
;HKEY_CURRENT_CONFIG (HKCC). If you do not specify a root key, KiXtart will use
;HKEY_CURRENT_USER as the default.


$key='HKEY_CURRENT_USER\Software\','Golfing','Round1','Enterprise Terminal Server'

; A best practice for a production script would be to use var names that have some
; type of meaning that is easily understood.
$wr=FindMe($key) ; will return a code to @error: 0 for true, and 1 for not true
@error

Function FindMe($key)
Dim $ch,$found,$index,$valuename,$KeyF,$JoinKey,$Valuename2,$wr,$split,$inst

; Using $key[0] is optomistic. If a user missed a comma or similar typo the
; UDF would end abnormally.
; I'd have to think about how best to modify it without completely changing your code.
$ch=KeyExist($key[0]) ; Does the HKEY_CURRENT_USER_\Software\ exist?

; In many cases just an If $var is good enough, but be aware there are times that
; this is not sufficient and could lead to unexpected results.
if $ch

; This is along the lines of the $key[0] issue and you've set it so that there
; is a known value to start with which is good.
$found=0
$index=0 ;Setting values so the while-statement will not run forever..


while @error=0 and $found=0
$ValueName = ENUMKEY($key[0],$index)
if $ValueName = $Key[1] $found=1
; Need to be careful or at least aware of data types when concatenating data
$KeyF=$key[0],$ValueName
$JoinKey=Join($KeyF,'')
$wr=ReadValue($JoinKey,$Key[2])
$split=Split($wr,'|')
$wr=Join($split,' ')
$inst=Instr($wr,$Key[3])
if $inst exit 0 else exit 1 endif
else
$Index = $Index + 1
endif
loop
exit @error
endif

EndFunction

; Overall I'm not comfortable with the calls to array elements without
; some type of prior initialization as it could easily lead to a script abend.


NTDOCAdministrator
(KiX Master)
2006-09-23 01:08 AM
Re: KiXtart Learning Series - Round 01

REMINDER: These are My comments and they may not fully agree with other long term / old time scripters.

At this time if other Admins or Moderators or those with a good understanding of KiXtart want to comment please do, but don't post just to bump your post count.

Please be constructive in nature.


NTDOCAdministrator
(KiX Master)
2006-09-23 01:29 AM
Re: KiXtart Learning Series - Round 01

And for those who want a Golfed down version here is an example.

Note though that for production scripts they should contain meaningful variable names and perform at least minimal error checking as well as some amount of documentation on what the script is doing.


Code:
 IIF(InStr(ReadValue('HKCU\Software\Golfing','Round1'),'Enterprise|Terminal|Server|'),0,1)     



Gargoyle
(MM club member)
2006-09-23 03:29 AM
Re: KiXtart Learning Series - Round 01

Using the IIF statement works if you understand the returns to put in. Since you are allowed to put in anything you want, an explanation of why 0 and 1 are used are in order I believe.

The primary reason for bringing this up is that I would have used -1 and 0, and that comes from using KiXforms.


Gargoyle
(MM club member)
2006-09-23 03:29 AM
Re: KiXtart Learning Series - Round 01

Also would like to see your comments on Section/Solution 2 of my post.

Benny69
(MM club member)
2006-09-23 05:25 AM
Re: KiXtart Learning Series - Round 01

Gargoyle,
I can see what you are saying:

-1 = True
0 = False

I often think that way too but it is probably because I am more familiar with KiXforms.


NTDOCAdministrator
(KiX Master)
2006-09-23 05:26 AM
Re: KiXtart Learning Series - Round 01

Well I'm sure there are times when -1 is appropriate but in this case we're simply looking for a flag to show that we did not find the value so anything aside from 0 would indicate it was not found.

Comments for your 2nd solution similar to first with regards to using var names with meaning if used in larger script, though not the case here.

Good idea for splitting based upon Reg Type

Here is a method to remove the space from your output check.

$J = Trim(Join($S," "))


Gargoyle
(MM club member)
2006-09-23 07:58 AM
Re: KiXtart Learning Series - Round 01

Never thought of Trim (then again that is why I am participating here). Any insight on why the Ubound($S)-1 did not work?

My thinking was just grab one less element and all would be good, but it continued to error on me.


PaulyT
(Fresh Scripter)
2006-09-24 05:25 PM
Re: KiXtart Learning Series - Round 01

I learned something new! I am a util kix scripter and always trying to find better ways of getting the task done. I was going with that we were to find if 'Enterprise Terminal Server' was present.

Code:

DIM $Round1Value
$Round1Value = Split(ReadValue('HKCU\Software\Golfing', 'Round1'), '|')
? IIF(Trim(Join($Round1Value, ' ',UBound($Round1Value))) = 'Enterprise Terminal Server', 'Value Present', 'Value Not Present')



I like Learning Series better than Mini Golf. Following the principles of best coding practice can only improve beginners and intermediate scripters. I never had used IIF or Join in my production environment, but you know for sure I will review some of my scripts to see if they can be improved. You can teach an old dog new tricks. Good stuff!


Björn
(Korg Regular)
2006-09-25 09:10 AM
Re: KiXtart Learning Series - Round 01

Thank you Doc for this - It's really been a learing experience! I'll try to follow your feedback as much as I can!
But to get back to our discussion we had eariler regarding my array's, could you or someone else care to share the real way to handle 'em?

Quote:

I like Learning Series better than Mini Golf. Following the principles of best coding practice can only improve beginners and intermediate scripters. I never had used IIF or Join in my production environment, but you know for sure I will review some of my scripts to see if they can be improved. You can teach an old dog new tricks. Good stuff!




I agree! Never used join nor IIF either


Sealeopard
(KiX Master)
2006-09-26 05:57 AM
Re: KiXtart Learning Series - Round 01

Quote:

Never used join nor IIF either


Then you should see what wonders a SPLIT-JOIN can do