Shadow_van
(Fresh Scripter)
2004-03-10 11:25 PM
How to refer to a value of a variable as it ...

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!


Co
(MM club member)
2004-03-10 11:59 PM
Re: How to refer to a value of a variable as it ...

Try it with for.. each lus.

For each printer in printers
..
next


Bryce
(KiX Supporter)
2004-03-11 12:04 AM
Re: How to refer to a value of a variable as it ...

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



Bryce
(KiX Supporter)
2004-03-11 12:16 AM
Re: How to refer to a value of a variable as it ...

or...

Code:

$Printer1='user1','user2','user3'

if ascan($printer1,'user3')+1
? "user3 is in Printer1 variable"
endif




maciep
(Korg Regular)
2004-03-11 12:21 AM
Re: How to refer to a value of a variable as it ...

Many different ways to do it. But if you want to leave your variables the same, something like this might work:

Code:

for each $printer in split($printers, ",")
$ = execute('$$curPrinter = "$" + $printer')
? $curPrinter
if instr($curPrinter, @userid)
? "user gets this printer"
endif
next



Shadow_van
(Fresh Scripter)
2004-03-11 12:24 AM
Re: How to refer to a value of a variable as it ...

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


Shadow_van
(Fresh Scripter)
2004-03-11 12:33 AM
Re: How to refer to a value of a variable as it ...

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!


maciep
(Korg Regular)
2004-03-11 12:46 AM
Re: How to refer to a value of a variable as it ...

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



Shadow_van
(Fresh Scripter)
2004-03-11 12:51 AM
Re: How to refer to a value of a variable as it ...

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


Shadow_van
(Fresh Scripter)
2004-03-11 12:54 AM
Re: How to refer to a value of a variable as it ...

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.


maciep
(Korg Regular)
2004-03-11 01:02 AM
Re: How to refer to a value of a variable as it ...

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




Sealeopard
(KiX Master)
2004-03-11 05:12 AM
Re: How to refer to a value of a variable as it ...

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.


Shadow_van
(Fresh Scripter)
2004-03-11 07:54 AM
Re: How to refer to a value of a variable as it ...

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
:-)


Shadow_van
(Fresh Scripter)
2004-03-11 08:02 AM
Re: How to refer to a value of a variable as it ...

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 :-)


Richard H.Administrator
(KiX Supporter)
2004-03-11 10:14 AM
Re: How to refer to a value of a variable as it ...

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


Sealeopard
(KiX Master)
2004-03-11 03:22 PM
Re: How to refer to a value of a variable as it ...

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.


Shadow_van
(Fresh Scripter)
2004-03-11 04:41 PM
Re: How to refer to a value of a variable as it ...

2 Richard and Sealeopard.

Well guys,
You've convinced me :-)
Thanks for your support and suggestions!



Shadow_van
(Fresh Scripter)
2004-03-11 05:41 PM
Re: How to refer to a value of a variable as it ...


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!


Sealeopard
(KiX Master)
2004-03-11 05:58 PM
Re: How to refer to a value of a variable as it ...

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



Shadow_van
(Fresh Scripter)
2004-03-11 06:07 PM
Re: How to refer to a value of a variable as it ...

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?


Sealeopard
(KiX Master)
2004-03-11 06:10 PM
Re: How to refer to a value of a variable as it ...

Yes, I ran this code and the .INI file on my compter. Also, if you want help then you will have to state more than "it doesn't work!". What are the error messages? Is the .INI path correct? KiXtart version? What happens if you run it with DEBUG ON?

Les
(KiX Master)
2004-03-11 06:17 PM
Re: How to refer to a value of a variable as it ...

Quote:

split statement dosn't work



My guess is he is on a really old version.


maciep
(Korg Regular)
2004-03-11 06:41 PM
Re: How to refer to a value of a variable as it ...

What's with splitting, joining, splitting? why not just:

Code:

$ini='d:\printer.ini'
$printers=split(readprofilestring($ini,'Printers',''),chr(10))
for each $printer in $printers
$users=split(readprofilestring($ini,'Printers',$printer), ',')
if ascan($users,@USERID)+1
? @USERID+' gets printer '+$printer
endif
next




Les
(KiX Master)
2004-03-11 06:48 PM
Re: How to refer to a value of a variable as it ...

If his KiX is too old for Split(), then Ascan() would not yeild any better mileage.

maciep
(Korg Regular)
2004-03-11 06:52 PM
Re: How to refer to a value of a variable as it ...

I think split didn't work before because he was trying to split an array.

Shadow_van
(Fresh Scripter)
2004-03-11 06:54 PM
Re: How to refer to a value of a variable as it ...

Quote:

If his KiX is too old for Split(), then Ascan() would not yeild any better mileage.




Guys,
I started using Kixtart when it was 4.21. It cannot be TOO old, can it?
I check your codes with kixtart 4.22, so let's not consider that point.


Sealeopard
(KiX Master)
2004-03-11 07:01 PM
Re: How to refer to a value of a variable as it ...

A) The code works under Kixtrat v4.22
B) SPLIT-JOIN-SPLIT removes the potential spaces in the list of users and converts it into an array.
C) A "It doesn't work" statement is not helpful, provide details as requested in my previous post.


Shadow_van
(Fresh Scripter)
2004-03-11 07:12 PM
Re: How to refer to a value of a variable as it ...

Ok.
There's my c:\printers.ini file:
[Printers]
Printer1=andry,mark,gwen,nick
Printer2=raman,helen,dave
Printer3=wendy,mavis,nicole

I ran the following code:
Break On
?@STARTDIR
?@SCRIPTexe
?@KIX
?@SCRIPTDIR
$ini='c:\printer.ini'
$printers=split(readprofilestring($ini,'Printers',''),chr(10))
for each $printer in $printers
$users=split(readprofilestring($ini,'Printers',$printer), ',')
if ascan($users,@USERID)+1
? @USERID+' gets printer '+$printer

EndIf
Next
Sleep 1000

===========
I used the macros to give you some ideas about kixtart version etc. My USERID is andry

The Output I get from the script above is:

C:\Program Files\PrimalScript\andry\Scripts
KIX32.EXE
4.22
C:\Program Files\PrimalScript\andry\Scripts

In other words, it just prints the macroses and then goes to the SLEEP statemnt. I've traced the script and it appears that $users array is never built properly that's why it cannot find my userid in the $users array.

Thanks for spending your time on my issue.






Sealeopard
(KiX Master)
2004-03-11 07:15 PM
Re: How to refer to a value of a variable as it ...

That's why your .INI file is called C:\PRINTERS.INI and in the script you call C:\PRINTER.INI.

Shadow_van
(Fresh Scripter)
2004-03-11 07:27 PM
Re: How to refer to a value of a variable as it ...

Quote:

That's why your .INI file is called C:\PRINTERS.INI and in the script you call C:\PRINTER.INI.




It works now!

DAMN!
I ain't that stupid, I swear!
BUT I DID THAT MISTAKE!
Thanks a lot for your help!

One more time appreciate your help!