Ante
(Lurker)
2003-05-23 12:17 AM
FOR EACH fails with single-value array?

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?


ShawnAdministrator
(KiX Supporter)
2003-05-23 12:24 AM
Re: FOR EACH fails with single-value array?

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 ]


Sealeopard
(KiX Master)
2003-05-23 12:25 AM
Re: FOR EACH fails with single-value array?

As you don't post complete information, like how you define your array that fails, I am assuming you do this:
code:
$PrinterArray = 'Printer1'

This is not an array but a string. This would be an array
code:
$PrinterArray[0] = 'Printer1'

You can always do a
code:
$PrinterArray='test'

if not (vartype($PrinterArray) & 8192)
$tmp=$PrinterArray
redim preserve $PrinterArray[0]
$PrinterArray[0]=$tmp
endif

? 'a = '+$PrinterArray[0]



Kdyer
(KiX Supporter)
2003-05-23 12:26 AM
Re: FOR EACH fails with single-value array?

I think the PriMapState() UDF covers this.

Kent


ShawnAdministrator
(KiX Supporter)
2003-05-23 12:27 AM
Re: FOR EACH fails with single-value array?

my gawd - are we all of one mind or what - have we been hanging together too long ? [Wink]

Sealeopard
(KiX Master)
2003-05-23 12:33 AM
Re: FOR EACH fails with single-value array?

We still have to work on the resonse time. 7 minutes is definitely too long.

Ante
(Lurker)
2003-05-23 12:44 AM
Re: FOR EACH fails with single-value array?

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!


Sealeopard
(KiX Master)
2003-05-23 03:48 AM
Re: FOR EACH fails with single-value array?

I guess it's time to split it up into a login script and admin scripts. Also, take advantage of the UDFs posted in the UDF Forum.