Page 1 of 1 1
Topic Options
#124309 - 2004-08-02 11:44 PM How often may I declare vars?
GyroTwister Offline
Fresh Scripter

Registered: 2003-01-29
Posts: 15
Loc: Hoogeveen, The Netherlands
Hello, KiX community,

I've got a new problem at hand. Whilst writing my first UDF, I read the How to make a UDF by sealeopard .
I this 'How To' it was recommended that I wrote the code in most restrictive 'mode' available to KiX.

Also as a comment on an earlier snippet of code from me, there came as comment not to 'double quote' vars and macros.

So I tried to do this, underneath you'll find the code:
Code:

;Function CheckLog
;
;ACTION Log date, time, scriptname and logtext to registry
;
;AUTHOR P.C.E.P.M. Sanders (P_Style; philip@pcsanders.net)
;
;CONTRIBUTORS None
;
;VERSION 1.0
;
;DATE CREATED 2004/08/02
;
;DATE MODIFIED 2004/08/02
;
;KIXTART 4.22
;
;SYNTAX CheckLog ($LogTxt)
;
;PARAMETERS $LogTxt
; The text you want to have logged into the registry
;
;RETURNS 0
;
;REMARKS None
;
;DEPENDENCIES None
;
Function CheckLog($LogTxt)

Dim $Space, $XName, $LogKey

$LogKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Deployment\Logging'
$Space = Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32) + Chr(32)
$XName = SubStr ($Script, 1, Len($Script)-4) + Right ($Space, 15 - Len(SubStr ($Script, 1, Len($Script)-4)))


If @ERROR = 0
WriteValue ($LogKey,@DATE+' '+@TIME,$XName+''+$LogTxt,'REG_SZ')
Sleep 1
Else
Color r+/n
Box (4,24,8,45,'Full')
Color r+/n
At (5,26) 'ERROR ENCOUNTERED!'
At (7,26) 'CALL LINE OPERATOR'
WriteValue ($LogKey,@DATE+' '+@TIME,$XName+''+$LogTxt+' FAILED!!','REG_SZ')
Color w+/n
Do
Beep
Beep
Beep
Until KBHit()
EndIf
EndFunction



This is working well.

However I call scripts from a central script. This central script checks the registry wheter or not to install software. You'll find a snippet here:
Code:

:Platform
? ' Routines called from loadsetserver.....'
$KixScr = ReadValue ($ScrKey,'Platform.kix')
If @ERROR = 0 AND $KixScr = 'Installed'
GoTo NLSet
Else
Call 'Z:\Scripts\Platform\Platform.kix'
EndIf

:NLSet
$KixScr = ReadValue ($ScrKey,'NLSet.kix')
If @ERROR = 0 AND $KixScr = 'Installed'
GoTo NLApps
Else
Call 'Z:\Scripts\NLSet\NLSet.kix'
EndIf



Before I execute this script I declare global and local vars, call udf's and udl's, set up the console like this:
Code:

; ===== OPTIONS / VARIABLES / USER DEFINED FUNCTION / USER DEFINED LIBRARIES =========
; ----- Set Options ------------------------------------------------------------------
SetOption ('ASCII','ON')
SetOption ('HideCursor','ON')
SetOption ('WrapAtEOL','ON')
SetOption ('Explicit','On')
SetOption ('NoVarsInStrings','On')
SetOption ('CaseSensitivity','On')

; ----- Call Required UDF's and UDL's ------------------------------------------------
Call 'Z:\SCRIPTS\UDF\CheckLog.UDF'

; ----- Declare Global Variables -----------------------------------------------------
Global $DepKey, $OldKey, $ScrKey, $CurVer, $WinLog, $PolKey, $DefUsr, $Script, $Server

SetConsole ('Maximize')

$DepKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Deployment'
$OldKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Deployment\OldMachine'
$ScrKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Deployment\Scripts'

$CurVer = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion'
$WinLog = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon'
$RunKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run'

$PolKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows'

$DefUsr = 'HKEY_USERS\EYInst'

$Script = @SCRIPTNAME

$Server = '\\LoadSrv'

; ----- Declare Local Variables ------------------------------------------------------
Dim $KixScr, $LoadSetRun

; ----- Set Up Console ---------------------------------------------------------------
SetConsole ('Maximize')
Color w+/n

; ----- Setting registry keys for installation ---------------------------------------
WriteValue ($ScrKey,@SCRIPTNAME,"Busy","REG_SZ")

; ----- Which script is called -------------------------------------------------------
CLS
Color y+/n
? ' '
? ' === ===> Running @SCRIPTNAME <=== ==='
Color w+/n



This last section I intend to make this a sort of 'default' for every script which is called. So every script will start with these lines.

Depending on the script local vars will differ.

Is it wise to do so? Is there a better way to make this happen?

I hope you can come up with some answers and maybe raise new questions.

t.i.a.

_________________________
-------------------------------------------------------------- With kind regards, GyroTwister

Top
#124310 - 2004-08-03 01:08 AM Re: How often may I declare vars?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Looks you are on the right track. I am curious about your use of the Z: drive as a location for scripts. I would think that using @Lserver or other variable that uses a UNC instead of a drive letter would be preferable to me.

I would use something like:

for $x=1 to 15
$spaces = $spaces + chr(32)
next

instead of your very long line.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#124311 - 2004-08-03 02:41 AM Re: How often may I declare vars?
Howard Bullock Offline
KiX Supporter
*****

Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
Your subject does not really follow your post. You can declare a variable when it is appropriate to declare a variable. You can use scoping such that a variable inside a loop or script is no longer addressable outside that scope.

See this example of using the same variable in two different scopes.

http://www.kixtart.org/ubbthreads/showthreaded.php?Cat=&Number=98486
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#124312 - 2004-08-03 11:19 AM Re: How often may I declare vars?
GyroTwister Offline
Fresh Scripter

Registered: 2003-01-29
Posts: 15
Loc: Hoogeveen, The Netherlands
Hello, KiX community,

Als antwoord op:

Your subject does not really follow your post.




I'm sorry about that. But what I really want to know is how does declaring vars in each script affect my system resources?

Or does KiX act with the vars, in the same way as with UDF's as axplained by sealeopard in his How To? And thus it will generated nearly no overhead?


_________________________
-------------------------------------------------------------- With kind regards, GyroTwister

Top
#124313 - 2004-08-03 11:41 AM Re: How often may I declare vars?
Anonymous
Unregistered


you can try doing a loop:
sleep 2
@time " " @msecs ?
for $counter=1 to 100000000000
dim $var
next
@time " " @msecs ?

to see how much it really takes.

Top
#124314 - 2004-08-03 12:44 PM Re: How often may I declare vars?
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Local variables should be automatically destroyed when you leave their context. The resources they used will either be freed and returned, or re-used.

Global variables will remain all the time that the KiXtart executable that they were defined in is running.

With "Explicit" on, you may get an error if you redeclare a variable. To avoid this, choose a naming convention for your global variables which ensures that you are not likely to have a name clash. I used the form "$_filename_variablename" for global variables. Check out the FAQ forum for recommendations.

If you include UDFs, libraries or a common file that defines constants, use a technique which ensures that the variables will only be declared once.

For example, if your UDF is stored in a file called myudf.kix, use something like the following:
Code:
;
; MYUDF() - demo UDF file.

; Ensure we are only included once.
If IsDeclared($_MYUDF_INCLUDED) Exit 0 EndIf

; Define included global
Global $_MYUDF_INCLUDED

; Define globals used by this UDF
Global $_MYUDF_Counter

; UDF definition.
Function MyUDF()
$_MYUDF_Counter=$_MYUDF_Counter+1
"You have called MyUDF "+$_MYUDF_Counter+" times."+@CRLF
Exit 0
EndFunction



The IsDeclared() check will ensure that your variables are only declared once. Note, the function itself will be redefined as that happens during the parse phase.

Top
#124315 - 2004-08-03 07:27 PM Re: How often may I declare vars?
GyroTwister Offline
Fresh Scripter

Registered: 2003-01-29
Posts: 15
Loc: Hoogeveen, The Netherlands
Hello, KiX Community,

Thanks for all the replies. I have followed your recommendations.

I am glad such a thing as internet and the KiXtart forum exists.

_________________________
-------------------------------------------------------------- With kind regards, GyroTwister

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.066 seconds in which 0.036 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