#115867 - 2004-03-10 11:25 PM
How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
was another variable?
Hi everyone, I'm trying to do the following: I have a variable containing list of my printers:
$printers="Printer1,Printer2,Printer3"
And I have lists of users who should have access to those printers. These lists of users are kept in variables as well: $Printer1="user1,user2,user3" $Printer2="user5,user4,user2" $Printer3="user4,user1,user10"
And I want to add printers to users in following manner: $ind=$Printers While InStr($ind,',') <>0 $delta=InStr($ind,',') $prin=Substr($ind,1,$delta-1) $ind=Right($ind,Len($ind)-$delta) If Instr($prin,@USERID)>0 $prn_name=Left($prin,InStr($prin,',')-1) AddPrinterConnection ("\\server\"+$prn_name) Else ? 'No printers for this pal!' EndIf Loop
The problem I have is that when I get the printers name (say printer1) and store it as $prin variable, I get $prin value = printer1. But I want it to be "user1,user2,user3" in other words, I want to refer $prin variable which points to $printer1 variable and get the value of $printer1.
Thanks!
|
Top
|
|
|
|
#115869 - 2004-03-11 12:04 AM
Re: How to refer to a value of a variable as it ...
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
take a look at the array commands. DIM, GLOBAL, SPLIT, UBOUND, JOIN, ASCAN....
Code:
$Printer1="user1,user2,user3"
? "user1 =" + split($printer1,",")[0] ? "user2 =" + split($printer1,",")[1] ? "user3 =" + split($printer1,",")[2]
or... just define your variables as an array in place of the string that youa re doing now.
Code:
$Printer1= "user1","user2","user3" for each $user in $printer1 ? $user next
Bryce
|
Top
|
|
|
|
#115870 - 2004-03-11 12:16 AM
Re: How to refer to a value of a variable as it ...
|
Bryce
KiX Supporter
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
or...
Code:
$Printer1='user1','user2','user3'
if ascan($printer1,'user3')+1
? "user3 is in Printer1 variable"
endif
|
Top
|
|
|
|
#115872 - 2004-03-11 12:24 AM
Re: How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
thanks, HOWEVER, the real problem is that in this row If Instr($prin,@USERID)>0 my code belives that $prin=printers, whereas i need it to belive that $prin==>$printer1==> user1,user2,user3
Thanks
|
Top
|
|
|
|
#115873 - 2004-03-11 12:33 AM
Re: How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
this code doesn't work at all if i replace split(#printers,',') with $printers, it'll work but the outout will be: $printer1 $printer2 $printer3
whreas i expect this particular code to return: user1,user2,user3 user5,user4,user2 user4,user1,user10
Thanks!
|
Top
|
|
|
|
#115875 - 2004-03-11 12:51 AM
Re: How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
Quote:
or...
Code:
$Printer1='user1','user2','user3'
if ascan($printer1,'user3')+1 ? "user3 is in Printer1 variable" endif
the trick is that i want to calculate the string name (printer1,printer2, etc.) dynamicaly
|
Top
|
|
|
|
#115876 - 2004-03-11 12:54 AM
Re: How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
Quote:
Where did you get '#' from? Anyway my, bad, try this:
Code:
for each $printer in split($printers, ",") $curPrinter = "$" + $printer $ = execute('$$curPrinter = $curPrinter') ? $curPrinter if instr($curPrinter, @userid) ? "user gets this printer" endif next
sorry, I meant $printers. split statement dosn't work in this code. But that's not critical. Which is more important that I get absolutely the same output.
|
Top
|
|
|
|
#115877 - 2004-03-11 01:02 AM
Re: How to refer to a value of a variable as it ...
|
maciep
Korg Regular
Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
|
What do you mean the split statement doesn't work. What version of kix are you using? When i run this on my machine, i get the right output.
Code:
$printers="Printer1,Printer2,Printer3" $Printer1 = "user1, user2, Eric" $Printer2 = "user1, user2, user4" $Printer3 = "user1, Eric, user8, user2" for each $printer in split($printers, ",") $curPrinter = "$" + $printer $ = execute('$$curPrinter = $curPrinter') ? $curPrinter if instr($curPrinter, @userid) ? "user gets this printer" endif next
Output:
Quote:
user1, user2, Eric user gets this printer user1, user2, user4 user1, Eric, user8, user2 user gets this printer
_________________________
Eric
|
Top
|
|
|
|
#115878 - 2004-03-11 05:12 AM
Re: How to refer to a value of a variable as it ...
|
Sealeopard
KiX Master
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|
This whole thing is badly designed. This type of logic is ideal for .INI files. this will allow you to sue some ratehr generic code and make changes to the printer mappings in the separate .INi file instead of hacking the code. Example: Code:
; .INI file [Printers] Printer1=user1, user2, Eric Printer2=user1, user2, user4 Printer3=user1, Eric, user8, user2
KiXtart code: Code:
$printers=split(readprofilestring('printers.ini','Printers',''),chr(10) for each $printer in $printers $users=split(readprofilestring('printers.ini','Printers',$printer),chr(10) if ascan($users,@USERID)+1 ? @USERID+' gets printer '+$printer endif next
It's not a best practive to dynamically generate variables, especially as variables generally do not represent information. Information is normally the value assigned to a variable, not the variable name itself.
_________________________
There are two types of vessels, submarines and targets.
|
Top
|
|
|
|
#115879 - 2004-03-11 07:54 AM
Re: How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
Quote:
What do you mean the split statement doesn't work. What version of kix are you using? When i run this on my machine, i get the right output.
Code:
$printers="Printer1,Printer2,Printer3" $Printer1 = "user1, user2, Eric" $Printer2 = "user1, user2, user4" $Printer3 = "user1, Eric, user8, user2" for each $printer in split($printers, ",") $curPrinter = "$" + $printer $ = execute('$$curPrinter = $curPrinter') ? $curPrinter if instr($curPrinter, @userid) ? "user gets this printer" endif next
Output:
Quote:
user1, user2, Eric user gets this printer user1, user2, user4 user1, Eric, user8, user2 user gets this printer
Thanks a lot! I don't know what was wrong on my PC, but this code didn't work (it was erroring out on split command). However, the troubles I had with my variables solved now. Thanks!
PS. btw, you don't need split if you define printers variable like this : $printers=Printer1,Printer2,Printer3 and then: for each $printer in $printers :-)
|
Top
|
|
|
|
#115880 - 2004-03-11 08:02 AM
Re: How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
Quote:
This whole thing is badly designed. This type of logic is ideal for .INI files. this will allow you to sue some ratehr generic code and make changes to the printer mappings in the separate .INi file instead of hacking the code.
Well, you'll eventually will edit either INI file or the scrpt itself. What's the difference? Moreover, the "genetic code" that I need my dynamic variables for, will let add new printers to the script in a very easy way - I'll need to add one more printer variable with list of the users and add the printer name to the printers list. Thanks for ideas anyways! I really appreciate them!
PS. If your're interested I can show the final login script when it's done. You'll see it has more sense than it appears to :-)
|
Top
|
|
|
|
#115881 - 2004-03-11 10:14 AM
Re: How to refer to a value of a variable as it ...
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
Well, you'll eventually will edit either INI file or the scrpt itself. What's the difference?
It is a basic question of design philosophy.
One is a program (script) and the other is a data source. It is a question of change control, limiting the effect of exceptions and introducing lines of demarcation or responsibility.
Including what is quite clearly "data" hard coded in a script is a bad idea. Here are a couple of obvious reasons:
- It's hard to add a front-end to automate the update of the information if it is hard-coded in the script.
- It is trivial to add a front end to update the information if it is stored in a data source.
- Changing a script runs the risk of introducing errors which will make the script fail or behave unexpectedly. These sort of errors may take time to surface and diagnose. The scope of the problems that may surface cannot be limited.
- Changes to a data source are simpler and finite in scope. They are less likely to be incorrect, and can be checked programatically to ensure that they are consistant and sensible
The more often you are going to make changes, the more often you are likely to introduce errors.
Now, in your situation you may be the only person who will ever update the script, and the updates may be few and far between. In this case it may not suit you to change your methodology - however switching to an INI file requires very little change and you will thank us for it later
|
Top
|
|
|
|
#115882 - 2004-03-11 03:22 PM
Re: How to refer to a value of a variable as it ...
|
Sealeopard
KiX Master
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|
Quote:
PS. btw, you don't need split if you define printers variable like this : $printers=Printer1,Printer2,Printer3
Which is well documented in the KiXtart Manual, however it's a best practice to always enclose strings in quotes (single quotes preferred) unless you're competing in KiXgolf.
What you are doing is horrible inefficient, prone to errors, and tough to debug. Just imagine supporting 100+ users, where the login script has to create 100+ distinct variables that do not contain any valuable information anyway. Data should be stored separately from script logic.
_________________________
There are two types of vessels, submarines and targets.
|
Top
|
|
|
|
#115883 - 2004-03-11 04:41 PM
Re: How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
2 Richard and Sealeopard.
Well guys, You've convinced me :-) Thanks for your support and suggestions!
|
Top
|
|
|
|
#115884 - 2004-03-11 05:41 PM
Re: How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
Quote:
Example: Code:
; .INI file [Printers] Printer1=user1, user2, Eric Printer2=user1, user2, user4 Printer3=user1, Eric, user8, user2
KiXtart code: Code:
$printers=split(readprofilestring('printers.ini','Printers',''),chr(10) for each $printer in $printers $users=split(readprofilestring('printers.ini','Printers',$printer),chr(10) if ascan($users,@USERID)+1 ? @USERID+' gets printer '+$printer endif next
it doesn't work for me : could you try to run it on your computer with your userid in one of the printers rows and see if you're "getting" a printer.
THanks again!
|
Top
|
|
|
|
#115885 - 2004-03-11 05:58 PM
Re: How to refer to a value of a variable as it ...
|
Sealeopard
KiX Master
Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
|
Provide the complete path to the .INI file and try this modified code:
Code:
$ini='d:\printer.ini'
$printers=split(readprofilestring($ini,'Printers',''),chr(10))
for each $printer in $printers
$users=split(join(split(readprofilestring($ini,'Printers',$printer)),''),',')
if ascan($users,@USERID)+1
? @USERID+' gets printer '+$printer
endif
next
Edited by sealeopard (2004-03-11 07:03 PM)
_________________________
There are two types of vessels, submarines and targets.
|
Top
|
|
|
|
#115886 - 2004-03-11 06:07 PM
Re: How to refer to a value of a variable as it ...
|
Shadow_van
Fresh Scripter
Registered: 2004-03-10
Posts: 13
|
Quote:
Provide the complete path to the .INi file and try this midified code: Code:
$ini='d:\printer.ini' $printers=split(readprofilestring($ini,'Printers',''),chr(10)) for each $printer in $printers $users=split(join(split(readprofilestring($ini,'Printers',$printer)),''),',') if ascan($users,@USERID)+1 ? @USERID+' gets printer '+$printer endif next
no luck does it work on your computer?
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 918 anonymous users online.
|
|
|