Page 1 of 1 1
Topic Options
#75152 - 2003-05-23 12:17 AM FOR EACH fails with single-value array?
Ante Offline
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 Offline
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
#75154 - 2003-05-23 12:25 AM Re: FOR EACH fails with single-value array?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
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]

_________________________
There are two types of vessels, submarines and targets.

Top
#75155 - 2003-05-23 12:26 AM Re: FOR EACH fails with single-value array?
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
I think the PriMapState() UDF covers this.

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#75156 - 2003-05-23 12:27 AM Re: FOR EACH fails with single-value array?
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
my gawd - are we all of one mind or what - have we been hanging together too long ? [Wink]
Top
#75157 - 2003-05-23 12:33 AM Re: FOR EACH fails with single-value array?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
We still have to work on the resonse time. 7 minutes is definitely too long.
_________________________
There are two types of vessels, submarines and targets.

Top
#75158 - 2003-05-23 12:44 AM Re: FOR EACH fails with single-value array?
Ante Offline
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
#75159 - 2003-05-23 03:48 AM Re: FOR EACH fails with single-value array?
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
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.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 515 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.06 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org