|
|
|||||||
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! |
||||||||
|
|
|||||||
Try it with for.. each lus. For each printer in printers .. next |
||||||||
|
|
|||||||
take a look at the array commands. DIM, GLOBAL, SPLIT, UBOUND, JOIN, ASCAN.... Code:
or... just define your variables as an array in place of the string that youa re doing now. Code:
Bryce |
||||||||
|
|
|||||||
or... Code:
|
||||||||
|
|
|||||||
Many different ways to do it. But if you want to leave your variables the same, something like this might work: Code:
|
||||||||
|
|
|||||||
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 |
||||||||
|
|
|||||||
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! |
||||||||
|
|
|||||||
Where did you get '#' from? Anyway my, bad, try this: Code:
|
||||||||
|
|
|||||||
Quote: the trick is that i want to calculate the string name (printer1,printer2, etc.) dynamicaly |
||||||||
|
|
|||||||
Quote: 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. |
||||||||
|
|
|||||||
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:
Output: 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. Example: Code:
KiXtart code: Code:
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. |
||||||||
|
|
|||||||
Quote: 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 :-) |
||||||||
|
|
|||||||
Quote: 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 :-) |
||||||||
|
|
|||||||
Quote: 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:
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 |
||||||||
|
|
|||||||
Quote: 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. |
||||||||
|
|
|||||||
2 Richard and Sealeopard. Well guys, You've convinced me :-) Thanks for your support and suggestions! |
||||||||
|
|
|||||||
Quote: 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! |
||||||||
|
|
|||||||
Provide the complete path to the .INI file and try this modified code: Code:
|
||||||||
|
|
|||||||
Quote: no luck does it work on your computer? |
||||||||
|
|
|||||||
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? |
||||||||
|
|
|||||||
Quote: My guess is he is on a really old version. |
||||||||
|
|
|||||||
What's with splitting, joining, splitting? why not just: Code:
|
||||||||
|
|
|||||||
If his KiX is too old for Split(), then Ascan() would not yeild any better mileage. |
||||||||
|
|
|||||||
I think split didn't work before because he was trying to split an array. |
||||||||
|
|
|||||||
Quote: 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. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
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. |
||||||||
|
|
|||||||
That's why your .INI file is called C:\PRINTERS.INI and in the script you call C:\PRINTER.INI. |
||||||||
|
|
|||||||
Quote: 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! |