Page 2 of 2 <12
Topic Options
#193502 - 2009-04-17 08:37 PM Re: Multiple Scripts and Parsing [Re: Gargoyle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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
_________________________
Actually I am a Rocket Scientist! \:D

Top
#193503 - 2009-04-17 08:43 PM Re: Multiple Scripts and Parsing [Re: Gargoyle]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4396
Loc: New Jersey
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


Edited by Glenn Barnas (2009-04-17 08:47 PM)
Edit Reason: added disclaimer
_________________________
Actually I am a Rocket Scientist! \:D

Top
#193741 - 2009-05-08 06:37 PM Re: Multiple Scripts and Parsing [Re: Gargoyle]
crmsonknight Offline
Fresh Scripter

Registered: 2009-03-20
Posts: 17
Loc: NH USA
I have 100 users in this script
Top
#193746 - 2009-05-09 06:48 AM Re: Multiple Scripts and Parsing [Re: crmsonknight]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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
_________________________
Today is the tomorrow you worried about yesterday.

Top
#193763 - 2009-05-11 04:22 PM Re: Multiple Scripts and Parsing [Re: Gargoyle]
crmsonknight Offline
Fresh Scripter

Registered: 2009-03-20
Posts: 17
Loc: NH USA
Thanks for all the help guys! I hope I can pay it forward sometime!
Top
#193792 - 2009-05-12 03:00 PM Re: Multiple Scripts and Parsing [Re: crmsonknight]
crmsonknight Offline
Fresh Scripter

Registered: 2009-03-20
Posts: 17
Loc: NH USA
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!

Top
#193794 - 2009-05-12 03:19 PM Re: Multiple Scripts and Parsing [Re: crmsonknight]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
 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
_________________________
Today is the tomorrow you worried about yesterday.

Top
#193796 - 2009-05-12 03:41 PM Re: Multiple Scripts and Parsing [Re: Gargoyle]
crmsonknight Offline
Fresh Scripter

Registered: 2009-03-20
Posts: 17
Loc: NH USA
Because I seem to be stupid....lol can you explain the case 1 and sorry I had the EndSelect in my script.

Edited by crmsonknight (2009-05-12 03:44 PM)

Top
#193797 - 2009-05-12 03:50 PM Re: Multiple Scripts and Parsing [Re: crmsonknight]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
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.
_________________________
Today is the tomorrow you worried about yesterday.

Top
#193799 - 2009-05-12 04:23 PM Re: Multiple Scripts and Parsing [Re: Gargoyle]
crmsonknight Offline
Fresh Scripter

Registered: 2009-03-20
Posts: 17
Loc: NH USA
But other than that...it should "work" right?
Top
#193801 - 2009-05-12 07:11 PM Re: Multiple Scripts and Parsing [Re: crmsonknight]
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
From what I can see yes.
_________________________
Today is the tomorrow you worried about yesterday.

Top
Page 2 of 2 <12


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

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.066 seconds in which 0.027 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org