1.

Solve : visual basic script out of bounds?

Answer»

So I'm attempting to LOOP through a text file in vbs. The problem seems to be occurring when I'm grabbing the line of text. If someone more versed in visual basic could help me spot the error, it would be much appreciated.

Note: the file 'planets' is a text file with no extension. And %wvec_lim%=2

Code: [Select]' grav "%cd%\planets" %pos_x% %pos_y% %pos_z% "%cd%" %wvec_lim%
'
' Applies gravity for each planet withing range. Uses wvec_lim variable
' from files settings to determine range. if dis/grav < wvrc_lim then
' gravity is applied. Value of grav is taken from file settings as well.

dim file,x,y,z,working_x,working_y,working_z,fso,arrline,dis,WVEC,vec_x,vec_y,vec_z,x1,y1,z1,grav,wvec_lim,planets,listFile,NAME

file=Wscript.Arguments(0)
x=Wscript.Arguments(1)
y=Wscript.Arguments(2)
z=Wscript.Arguments(3)
cd1=Wscript.Arguments(4)
wvec_lim=CSng(Wscript.Arguments(5))
vec_x=0
vec_y=0
vec_z=0

wscript.echo "|"&file

' Loop through planets list
Set fso = CreateObject("Scripting.FileSystemObject")
Set listFile = fso.OpenTextFile(file)
do while not listFile.AtEndOfStream
line = listFile.ReadLine()

wscript.echo "|"&line

' setup variables
arrline=Split(line,",")
working_x=arrline(0)
working_y=arrline(1)
working_z=arrline(2)
name=arrline(3)
grav=arrline(5)

' Ignore all planets with 0 gravity
' These are comments in the file
If grav <> 0 Then

'find distance
dis=Sqr((x-working_x)*(x-working_x)+(y-working_y)*(y-working_y)+(z-working_z)*(z-working_z))
wvec=dis/grav


'determine adding
If wvec < wvec_lim Then

' Find Vector length
x1=round(((working_x-x)/dis/grav),2)
y1=round(((working_y-y)/dis/grav),2)
z1=round(((working_z-z)/dis/grav),2)

' Display Data
Wscript.echo name
Wscript.echo "wvec=" & round(wvec,2)
Wscript.echo "x1="&x1
Wscript.echo "y1="&y1
Wscript.echo "z1="&z1

' Add vector length to sum
vec_x=vec_x+x1
vec_y=vec_y+y1
vec_z=vec_z+z1

End If
End If
Loop

' Make corrections on possition
x=x+vec_x
y=y+vec_y
z=z+vec_z

' Write to Files
set objFSO=CreateObject("Scripting.FileSystemObject")
outFile=cd & "pos_x"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write x & vbCrLf
objFile.Close

outFile=cd & "pos_y"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write y & vbCrLf
objFile.Close

outFile=cd & "pos_z"
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.Write z & vbCrLf
objFile.Close
Code: (planets) [Select]
0,0,0,0,0,0,0,0, How to READ: x,y,z,name,danger,grav
0,0,0,0,0,0,0,0, danger 0=can't land, 1=can safely land, 2=crash
0,0,0,0,0,0,0,0, smaller the grav the harder the pull
0,0,0,0,0,0,0,0, 1 pt ~ 100,000 miles


0,0,0,0,0,0,0,0, Sol System
390000,470000,-38000,Sol,2,1
390000,470360,-37900,Mercury,1,50
390600,470070,-38100,Venus,1,30
390629,469700,-38070,Earth,1,10
388584,470000,-38030,Mars,1,10
394000,470836,-37970,Jupiter,0,5
381118,469990,-38000,Saturn,0,7
372136,469900,-37920,Uranus,0,20
390000,442012,-38000,Neptune,0,20
426662,470000,-40000,Pluto,1,100


0,0,0,0,0,0,0,0, Other planets
10,10,10,Abron,1,10
Code: (output) [Select]|T:\space\bin\planets
|

T:\space\bin\grav.vbs(31, 2) Microsoft VBScript runtime error: Subscript out of
range: '[number: 0]'

T:\space\bin\chkcrsh.vbs(29, 5) Microsoft VBScript runtime error: Subscript out
of range: '[number: 0]'
I have narrowed it down to these lines:
Set fso = CreateObject("Scripting.FileSystemObject")
Set listFile = fso.OpenTextFile(file)
do while not listFile.AtEndOfStream
line = listFile.ReadLine()
Somehow line is not getting setThe planets file has empty lines. They have no "," so Split returns an empty array. Attempting to index an empty array results in a Subscript out of range error on line 31. You can wrap the logic in the loop after splitting the string in an If so it is only processed if the line has elements. Or you could do the sme and test that the line isn't blank.
Thank you so much! I thought I had tried that before, but I had forgotten to remove the first line (which was blank). It works now.



Discussion

No Comment Found