#75152 - 2003-05-23 12:17 AM
FOR EACH fails with single-value array?
|
Ante
Lurker
Registered: 2003-05-22
Posts: 2
|
I'm experiencing a wierd situation with the FOR EACH command. When I use it with an array with multiple values, it works just as expected. However, when I run it hits an array with only a single value, it does nothing.
At the beginning of the script, a routine determines a user's location and sets an array with the names of the printers for that location. For example:
code:
$PrinterArray = "Printer1","Printer2","AndSoOn"
Then, later in the script, a printer subroutine runs which adds the printers. It looks like this:
code:
IF ISDECLARED($PrinterArray) FOR EACH $Printer IN $PrinterArray IF ADDPRINTERCONNECTION("\\$Printserver\$Printer") = 0 "Adding printer $Printer" ? ELSE "I couldn't set up the $Printer printer. Error: @Error" ? ENDIF NEXT
ELSE "There don't seem to be any printers to set up at your location." ? RETURN ENDIF
RETURN
This all works fine with two or more printers in the printer array. However, if the script behaves oddly when presented with an array with only one value. When that occurs, neither option in the IF THEN statement seems to evaluate true. It does not add the printer and state it has done so, which it would do if the ADDPRINTERCONNECTION returned a 0. Nor does it say it can't add the printer and return the error code.
Adding a second printer to the array solves the problem. So, as an ugly hack, in locations with only a single printer, I simply add it twice to the array, so:
code:
$PrinterArray = "Printer1","Printer1"
But that doesn't fit with my neatly-ordered view of the universe. Am I doing something wrong or strange?
|
Top
|
|
|
|
#75153 - 2003-05-23 12:24 AM
Re: FOR EACH fails with single-value array?
|
Shawn
Administrator
Registered: 1999-08-13
Posts: 8611
|
Yeah - because with the literal assignment technique your using, for example:
$Printers = "print1", "print2"
that does create an array, but this:
$Printers = "print1"
does not create an array, it creates a string - which cant be enumerated with for-each-in-next. So like you said, add a dummy var and check for that in your loop:
$Printers = "print1",""
or declare a true one single element array the hard (ugly) way:
Dim $Printers[0] $Printers[0] = "Print1"
or the other thing you could do is check for the variable "type" before you enter your loop, using the VarTypeName() function. If an array, do the for-each-in-next, if its a string, handle it as a string. I kinda like that approach myself.
-Shawn [ 23. May 2003, 00:26: Message edited by: Shawn ]
|
Top
|
|
|
|
#75155 - 2003-05-23 12:26 AM
Re: FOR EACH fails with single-value array?
|
Kdyer
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
I think the PriMapState() UDF covers this.
Kent
|
Top
|
|
|
|
#75156 - 2003-05-23 12:27 AM
Re: FOR EACH fails with single-value array?
|
Shawn
Administrator
Registered: 1999-08-13
Posts: 8611
|
my gawd - are we all of one mind or what - have we been hanging together too long ?
|
Top
|
|
|
|
#75158 - 2003-05-23 12:44 AM
Re: FOR EACH fails with single-value array?
|
Ante
Lurker
Registered: 2003-05-22
Posts: 2
|
Well, that explanation is certainly logical. Thanks for the many quick replies!
That's what you get when your little 20-line network script experiences 'feature creep', I suppose. I wrote the initial network script in batch, and it mostly mapped network drives, then it grew and grew and grew. Now, it's expected to determine a user's physical location based on their subnet (even if they're remote Citrix users who report the Citrix application server's IP address), then do all sorts of stuff to them like update their antivirus software, set their screensaver and otherwise fool around in the registry, and other scary stuff like rebooting the computer, on Windows 9x through XP machines. It's now over a thousand lines long! It's a testament to Kix and this board that I was able to write such a script sucessfully with very little programming experience.
Thanks again!
|
Top
|
|
|
|
Moderator: Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart
|
0 registered
and 515 anonymous users online.
|
|
|