crmsonknight
(Fresh Scripter)
2009-04-14 03:08 PM
Multiple Scripts and Parsing

I am trying to write a script that will call or shell to another script based on @USERID then return back to the original script. However, I need to parse part of that userid and use that to base which script it calls. Is there a way to do this?


Thank you all in advance!!!


Glenn BarnasAdministrator
(KiX Supporter)
2009-04-14 03:14 PM
Re: Multiple Scripts and Parsing

Do you have any code you're playing with (real or pseudo) that will give us a better idea of what you need?

Glenn


crmsonknight
(Fresh Scripter)
2009-04-14 03:32 PM
Re: Multiple Scripts and Parsing

; UserID Settings
Select
Case @USERID = "601"
Call "NODE601.kix"
Case @USERID = "602"
Call "NODE602.kix"
Case @USERID = "603"
Call "NODE603.kix"
Case 1
? "Warning! Unknown User"
? "Please Call The Help Desk!"
EndSelect

The full username is 601Agent_01 or 601Mgr_01 and I'd like to parse the first 3 numbers off of it.


Mart
(KiX Supporter)
2009-04-14 03:42 PM
Re: Multiple Scripts and Parsing

This should get you started.

 Code:
;Get the 1st, 2nd and 3rd character of the user id.
$id = SubStr(@USERID, 1, 3)

;See if the $id variable holds valid data.
If $id = "601" Or $id = "602" Or $id = "603"
	Call "NODE" + $id + ".kix"
Else
	? "Warning! Unknown User"
	? "Please Call The Help Desk!"
EndIf


Oh and btw please use code tags when posting code. The formatting will be preserved that way so it is easier to read especially with large scripts.


Glenn BarnasAdministrator
(KiX Supporter)
2009-04-14 05:06 PM
Re: Multiple Scripts and Parsing

Another tactic, which eliminates coding changes as new "nodes" are added:
 Code:
$File = 'NODE' + Left(@USERID, 3) + '.kix'
If Exist($File)
  Call $File
Else
  'Warning! Unknown user' @CRLF 'Please contact the help desk!' ?
EndIf

Adding a new script "NODE###.kix" is all you need to do to support another group of users.

Glenn


crmsonknight
(Fresh Scripter)
2009-04-14 05:37 PM
Re: Multiple Scripts and Parsing

Thanks Guys!!!!!! Those are perfect!!!

crmsonknight
(Fresh Scripter)
2009-04-14 05:56 PM
Re: Multiple Scripts and Parsing

Do you guys know if the variables stored in sub scripts are still valid in the main script? If I call script node601.kix from login.kix and I use a variable in node601.kix, can i make a call to it in login.kix. I think I would but just want to be sure!
Thanks!


Glenn BarnasAdministrator
(KiX Supporter)
2009-04-15 12:05 AM
Re: Multiple Scripts and Parsing

To be sure, declare the var as a global in your main script. Don't leave things to chance by not declaring them.

Works great, actually.. I declare $PROCNAME as a global in my main script. Subscripts immediately set $PROCNAME="myname" and load some UDFs. Each UDF in a subscript begins with "MyName", so I can make calls (via Exec) like this:
 Code:
$Fn = $MyName + '_Init()'
Exec($Fn)

This initializes the module, which often calls other functions from the sub-script. I have an app that dynamically loads and defines specific capabilities in this way - add a file, you add functionality. Delete a file, remove functionality.

Glenn


crmsonknight
(Fresh Scripter)
2009-04-15 03:28 PM
Re: Multiple Scripts and Parsing

Ok...I'm a bit new to scripting....How do you declare things as globals?

Glenn BarnasAdministrator
(KiX Supporter)
2009-04-15 04:00 PM
Re: Multiple Scripts and Parsing

Global $VarName
vs
Dim $VarName

Undeclared vars default to globals and will get you into trouble. ALWAYS declare your vars via GLOBAL or DIM commands.

Use of $=SetOption('Explicit', 'On') will prevent the script from running with undeclared vars. There are several SetOption settings that will enforce good coding habits, including NoVarsInStrings and NoMacrosInStrings. WrapAtEOF is also recommended for console scripts.

Use of KGen (or at least the Sanity() UDF) will report all undeclared vars.

KGen is part of the Kix-Dev package on my web site, and includes the Sanity feature. It's a UDF resolver (kind of like a linker).

Glenn


Mart
(KiX Supporter)
2009-04-15 04:31 PM
Re: Multiple Scripts and Parsing

 Originally Posted By: Glenn Barnas

....
WrapAtEOF is also recommended for console scripts.
....


WrapAtEOL would be even better \:D

Sorry Glenn, just rattling your chain.


crmsonknight
(Fresh Scripter)
2009-04-15 04:43 PM
Re: Multiple Scripts and Parsing

ok...let me say I'm way new to scripting and still do not understand what you are saying guys...dumb it down for me please...looks like i need the spoon feeding...sorry

Mart
(KiX Supporter)
2009-04-15 04:56 PM
Re: Multiple Scripts and Parsing

LOL

If you do not declare the variables at the top of your script then KiXtart assumes that they are to be used as Global variables. So the variables can be used in the master script and child scripts until the kix32 or wkix32 session is ended by you or when the script is finished.

Declaring the variables will save you some headaches.

If you use Dim $var1, $var2, $var3 at the top of your script then the variables are available to the script that decaled them using the DIM command. If you replace DIM by GLOBAL then they are available for the master and all child scripts.

Using SetOption("Explicit","On") will tell kix that all variables MUST be declared and if they are not the script just wont run. This is just one of the little helpers to move to best practice scripting.


Glenn BarnasAdministrator
(KiX Supporter)
2009-04-16 04:49 AM
Re: Multiple Scripts and Parsing

EOL - yeah - right . . . Really meant "EOFL", since that damn Line was really getting on my nerves all day. ('specially with the damn chain rattling all the time!!) \:D

Glenn


Glenn BarnasAdministrator
(KiX Supporter)
2009-04-16 04:57 AM
Re: Multiple Scripts and Parsing

Sir Crimson

You might want to take a look at this post for a recent discsussion about coding style, variable declarations, and use of the (correct) SetOption parameters such as NoMacrosInStrings, NoVarsInStrings, and (my personal favorite) WrapAtEOL! ;\)

There are some other discussions and code critiques on the board that will help you understand why having a consistent coding style will help you, as well as the reasons for declaring vars and setting options in your scripts - especially as they get more complex.

Glenn


crmsonknight
(Fresh Scripter)
2009-04-17 06:45 PM
Re: Multiple Scripts and Parsing

ok, i think i'm starting to understand... so if I do the following:
SetOption("Explicit","On")
Global $NODE
Global $Cache
then call the var $Cache or $NODE later in the script....?
If I call a child script from a main, how do i get it to return to the main script? In the child, I've got several different goto's and what I was thinking was that if I have a tag in the main called common and in the child say goto common that should work right?


Glenn BarnasAdministrator
(KiX Supporter)
2009-04-17 08:00 PM
Re: Multiple Scripts and Parsing

Using GOTO is bad on many levels. Lets say, for example, that I said "GOTO Post-109423" to get your answer. Where would you reply? That thread?, this original thread? or some other thread entirely?

GOSUB is a little bit better, and a good way to get used to functions. I could say "GOSUB Post-109423" to get more info, but when you're done reading that post, RETURN to the original post. GOSUB calls a SUBROUTINE by name that performs some task and returns to the original location (line following the gosub, actually). GOSUBs are OK, but you can't exchange data with them. They are part of the main program and have no isolation of variable names or data. Subroutines MODIFY THE ORIGINAL DATA.

Functions are the best because they provide a completely isolated environment - you can pass data to a function and it will get a COPY of the data. It can perform calculations and manipulate that data any way it wants without affecting the original data. You can return a COPY of the function's data back to the main program (where it can replace its copy with the modified data if you choose). Functions can also return result codes via EXIT, which can be checked by examining @ERROR, making it easy to tell if a function did its job successfully or not.

I'll post some code that will provide a few simple examples later this evening.. You should get the concept more clearly from those.

Glenn


crmsonknight
(Fresh Scripter)
2009-04-17 08:07 PM
Re: Multiple Scripts and Parsing

Thank you I appreciate your time and input. This is what I'm trying to do.

 Code:
If @USERID = "601Mgr_03" GOTO RM601C
endif

:RM6010
writevalue ("HKEY_CURRENT_USER\Environment", "Station", "601100", "REG_SZ")
writevalue ("HKEY_CURRENT_USER\Environment", "Extension", "601100", "REG_SZ")
writevalue ("HKEY_CURRENT_USER\Control Panel\Mouse", "SwapMouseButtons", "1", "REG_SZ")
goto Common

:Common
writevalue ("HKEY_CURRENT_USER\Environment", "Formcache", "C:\Cache\cache%station%", "REG_SZ")
writevalue ("HKEY_CURRENT_USER\Environment", "Extension", %Ext%, "REG_SZ")
goto end


crmsonknight
(Fresh Scripter)
2009-04-17 08:08 PM
Re: Multiple Scripts and Parsing

ok to my last post I realize my userid doesn't match...it's a snippit of what i'm trying to do....assume that userid is the same as the tag RM6010 please.

Gargoyle
(MM club member)
2009-04-17 08:14 PM
Re: Multiple Scripts and Parsing

Dependent on how many users that need custom settings...

 Code:
Select
  Case @UserID = "601MGR_03"
    writevalue ("HKEY_CURRENT_USER\Environment", "Station", "601100", "REG_SZ")
    writevalue ("HKEY_CURRENT_USER\Environment", "Extension", "601100", "REG_SZ")
    writevalue ("HKEY_CURRENT_USER\Control Panel\Mouse", "SwapMouseButtons", "1", "REG_SZ")
  Case @UserID = "602MGR_01"
    writevalue ("HKEY_CURRENT_USER\Environment", "Station", "601100", "REG_SZ")
    writevalue ("HKEY_CURRENT_USER\Environment", "Extension", "601100", "REG_SZ")
    writevalue ("HKEY_CURRENT_USER\Control 
  Case 1
    ;Maybe write to a log file or just do nothing
EndSelect

LoadCommon()

Function LoadCommon()
  writevalue ("HKEY_CURRENT_USER\Environment", "Formcache", "C:\Cache\cache%station%", "REG_SZ")
  writevalue ("HKEY_CURRENT_USER\Environment", "Extension", %Ext%, "REG_SZ")
  $Function = @Error
EndFunction


Glenn BarnasAdministrator
(KiX Supporter)
2009-04-17 08:37 PM
Re: Multiple Scripts and Parsing

Try this basic example to illustrate the use of Subroutines and Functions relating to variable scope (visibility).
 Code:
Break On

Global $MYNAME					; global var to illustrate scope
Dim $						; temp variable
Dim $TestVar, $NewTestVar			; test variables


$ = SetOption('Explicit', 'On')

; define the global with some data
$MYNAME = 'My name is ' + @WKSTA
'Set MYNAME to "' $MYNAME '"' ?


; set the variable to a known value & display it
$TestVar = @DATE
'Set TestVar to ' $TestVar ?


; Call a subroutine that will display and modify the original $TestVar value
'Calling SUB1' ?
Gosub 'Sub1'
? 'back in Main - TestVar now contains contains: ' $TestVar ?


; Call a function, pass it a copy of $TestVar for it to display and modify
; store the modified value in a new variable name
$NewTestVar = Func1($TestVar)
? 'back in Main - After calling Func1, TestVar contains "' $TestVar '"' ?
'and NewTestVar contains "' $NewTestVar '".' ?


; End of script - subroutines follow
Exit 0

:Sub1

  ? 'Running Sub1' ?
  ; proof of global visibility
  'MYNAME contains "' $MYNAME '"' ?

  ; proof of local visibility
  'TestVar contains: ' $TestVar 
  '... Resetting it to ' @TIME ?
  $TestVar = @TIME
  Return


Function Func1($_MyTestVar)

  ? 'Running Func1' ?
  ; proof of global visibility
  If IsDeclared($MYNAME )
    'MYNAME is visible and contains "' $MYNAME '".' ?
  Else
    'MYNAME is not visible!' ?
  EndIF

  ; proof of no external local visibility 
  If IsDeclared($TestVar)
    'TestVar is visible and contains "' $TestVar '".' ?
  Else
    'TestVar is not visible (as expected)!' ?
  EndIF

  ; proof of local visibility of passed value
  'The MyTestVar contains "' $_MyTestVar '". It should have data!' ?

  ; Changing local copy
  $_MyTestVar = @TICKS
  'MyTestVar now contains "' $_MyTestVar '". It should be different!' ?

  ; Return the modified data
  $Func1 = $_MyTestVar
  ; return success
  Exit 0

EndFunction
Should run "as is".

Glenn


Glenn BarnasAdministrator
(KiX Supporter)
2009-04-17 08:43 PM
Re: Multiple Scripts and Parsing

So, borrowing on Gargoyle's example, you could
 Code:
Function DoStuff($1,$2)
  Dim $
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'Station', $1, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'Extension', $2, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Control Panel\Mouse", "SwapMouseButtons", "1", "REG_SZ")
  Exit 0
EndFunction

Select
 Case @USERID = "xxx" DoStuff('601100', '601100')
 Case @USERID = "yyy" DoStuff('601200', '601200')
EndSelect
Basically, you are doing the same thing many times with slightly different values. You pass those unique values to the function, which plugs them in to the proper place.

This is an EXAMPLE which may not be complete for your needs and does not have any error checking.. it's a place for you to start, not finish! \:\)

Glenn


crmsonknight
(Fresh Scripter)
2009-05-08 06:37 PM
Re: Multiple Scripts and Parsing

I have 100 users in this script

Gargoyle
(MM club member)
2009-05-09 06:48 AM
Re: Multiple Scripts and Parsing

Not a big deal. Make an INI file and use READPROFILESTRING

INI file

[Header]
User1=Val1,Val2,Val3
User2=Val1,Val2,Val3

etc...

Dependent on what values you need to store and how consistent they are.

Another option

[HEADER]
User1=Type1
User2=Type2
User3=Type1
User4=Type2

This makes things easier to manipulate without having to change the script


crmsonknight
(Fresh Scripter)
2009-05-11 04:22 PM
Re: Multiple Scripts and Parsing

Thanks for all the help guys! I hope I can pay it forward sometime!

crmsonknight
(Fresh Scripter)
2009-05-12 03:00 PM
Re: Multiple Scripts and Parsing

Can one of you tell me what I did wrong in this script please?
 Code:
Function UsrEnv($1,$2,$3,$4,5$)
  Dim $
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'Station', $1, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'Extension', $2, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'PTCPort', $3, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'PUIPort', $4, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'Formcache', $5, "REG_SZ")
  Exit 0
EndFunction

Select
 Case @USERID = "608Mgr_100" UsrEnv('608100', '608100', '12800', '13000', 'C:\Cache\cache608100')
 Case @USERID = "608Mgr_101" UsrEnv('608101', '608101', '12801', '13001', 'C:\Cache\cache608101')
 Case @USERID = "608Mgr_102" UsrEnv('608102', '608102', '12802', '13002', 'C:\Cache\cache608102')
 Case @USERID = "608Mgr_103" UsrEnv('608103', '608103', '12803', '13003', 'C:\Cache\cache608103')
 Case @USERID = "608Mgr_104" UsrEnv('608104', '608104', '12804', '13004', 'C:\Cache\cache608104')
 Case @USERID = "608Mgr_105" UsrEnv('608105', '608105', '12805', '13005', 'C:\Cache\cache608105')

I seem to be pulling what little hairs I have left and can't make heads or tales out of it!


Gargoyle
(MM club member)
2009-05-12 03:19 PM
Re: Multiple Scripts and Parsing

 Code:
Function UsrEnv($1,$2,$3,$4,5$)
  Dim $
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'Station', $1, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'Extension', $2, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'PTCPort', $3, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'PUIPort', $4, "REG_SZ")
  $ = writevalue ("HKEY_CURRENT_USER\Environment", 'Formcache', $5, "REG_SZ")
EndFunction

Select
 Case @USERID = "608Mgr_100" 
  UsrEnv('608100', '608100', '12800', '13000', 'C:\Cache\cache608100')
 Case @USERID = "608Mgr_101" 
  UsrEnv('608101', '608101', '12801', '13001', 'C:\Cache\cache608101')
 Case @USERID = "608Mgr_102" 
  UsrEnv('608102', '608102', '12802', '13002', 'C:\Cache\cache608102')
 Case @USERID = "608Mgr_103" 
  UsrEnv('608103', '608103', '12803', '13003', 'C:\Cache\cache608103')
 Case @USERID = "608Mgr_104" 
  UsrEnv('608104', '608104', '12804', '13004', 'C:\Cache\cache608104')
 Case @USERID = "608Mgr_105" 
  UsrEnv('608105', '608105', '12805', '13005', 'C:\Cache\cache608105')
 Case 1
  'No Match Found' ?
EndSelect


Try it this way. Note the removal of "Exit 0" and the Addition of "EndSelect" as well as the Case 1 to catch all the fall out


crmsonknight
(Fresh Scripter)
2009-05-12 03:41 PM
Re: Multiple Scripts and Parsing

Because I seem to be stupid....lol can you explain the case 1 and sorry I had the EndSelect in my script.

Gargoyle
(MM club member)
2009-05-12 03:50 PM
Re: Multiple Scripts and Parsing

Case 1 just catches anything that did not meet the criteria of the CASE statements. This allows you to see that your input did not match anything so that you know what is going on.

crmsonknight
(Fresh Scripter)
2009-05-12 04:23 PM
Re: Multiple Scripts and Parsing

But other than that...it should "work" right?

Gargoyle
(MM club member)
2009-05-12 07:11 PM
Re: Multiple Scripts and Parsing

From what I can see yes.