Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple question about UBound behavior

Status
Not open for further replies.
Joined
Sep 20, 1999
Messages
3,824
Location
US
Ok, I'm working through a book and came upon this code example, but I don't understand its output:

Code:
TxtFile = "C:\ServersAndServices.txt"
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    (TxtFile, ForReading)
Do Until objTextFile.AtEndOfStream
boundary = Ubound(arrServiceList)
wscript.Echo "upper boundary = " & boundary
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    WScript.Echo "Server name: " & arrServiceList(0)
    For i = 1 To Ubound(arrServiceList)
        WScript.Echo "Service: " & arrServiceList(i)
    Next
Loop
wscript.Echo "upper boundary = " & boundary
WScript.Echo("all done")

Here are the contents of the input file:
Code:
VORPAL,netlogon
VORPAL,netlogon,dhcpclient,spooler
VORPAL,netlogon,dhcpclient,spooler,iisadmin,w3svc

And here's what I get when I run it:

Code:
upper boundary =
Server name: VORPAL
Service: netlogon
upper boundary = 1
Server name: VORPAL
Service: netlogon
Service: dhcpclient
Service: spooler
upper boundary = 3
Server name: VORPAL
Service: netlogon
Service: dhcpclient
Service: spooler
Service: iisadmin
Service: w3svc
upper boundary = 3
all done

I'm expecting the value of UBound to be 4, not 3. I understand that arrays are zero-based, so it makes sense that if there were five items stored in the array, then the UBound value would be 4. I just can't fathom why it would be 3.

Can someone explain this?

Thanks,

ShackDaddy
Shackelford Consulting
 
In fact, boundary is not calculated for the last line read ...
My suggestion:
Code:
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    WScript.Echo "Server name: " & arrServiceList(0)
    For i = 1 To Ubound(arrServiceList)
        WScript.Echo "Service: " & arrServiceList(i)
    Next
    WScript.Echo "upper boundary = " & Ubound(arrServiceList)
Loop
WScript.Echo("all done")

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Code:
TxtFile = "C:\ServersAndServices.txt"
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    (TxtFile, ForReading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    WScript.Echo "Server name: " & arrServiceList(0)
    For i = 1 To Ubound(arrServiceList)
        WScript.Echo "Service: " & arrServiceList(i)
    Next
    intBoundary = Ubound(arrServiceList)
    WScript.Echo "Upper Boundary: " & intBoundary
Loop
WScript.Echo("All done")

This'll probably come back with better results. You're obtaining your UBound from the priod arrServiceList.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top