Page 1 of 1 1
Topic Options
#166451 - 2006-08-25 07:04 PM Script error: array reference out of bounds!
ledzepe Offline
Fresh Scripter

Registered: 2005-12-06
Posts: 26
Hi everyone and advance thanks for those would help. Here's my script, p.csv is a multi row/line file. I get the error "array reference out of bounds!" after it has read the whole file.

- - - - - - - - - - -
Dim $Line[39]
If Open(1,"p.csv",2) = 0

WHILE @ERROR = 0
$Line = Split(readline(1),",")
? $Line[0] + $Line[3] + $Line[19] + $Line[21]
LOOP

endif

Top
#166452 - 2006-08-25 07:29 PM Re: Script error: array reference out of bounds!
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
do a "if ubound($line)>21 ... endif" arounf the print statement.

You can't guarantee there are 21 elements on each line

Top
#166453 - 2006-08-25 08:22 PM Re: Script error: array reference out of bounds!
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
That'd be 22.
Top
#166454 - 2006-08-25 09:10 PM Re: Script error: array reference out of bounds!
ledzepe Offline
Fresh Scripter

Registered: 2005-12-06
Posts: 26
The p.csv file has 39 columns/elements. The script above displays columns 0,3,19 and 21 properly up to the very last row of data then displays the error above plus the line "? $Line[0] + $Line[3] + $Line[19] + $Line[21]". I should have added that earlier.
Top
#166455 - 2006-08-25 09:33 PM Re: Script error: array reference out of bounds!
ledzepe Offline
Fresh Scripter

Registered: 2005-12-06
Posts: 26
I got it, i added an "if $Line[0] = "" goto end1 endif" after the split command and that did the trick. i was getting the out of bounds error because it has reached the end of the row and there was nothing to display.
Top
#166456 - 2006-08-25 10:01 PM Re: Script error: array reference out of bounds!
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
NOOOOO! Don't use GOTO. Rad already gave you better code.
Top
#166457 - 2006-08-25 10:53 PM Re: Script error: array reference out of bounds!
Gargoyle Offline
MM club member
*****

Registered: 2004-03-09
Posts: 1597
Loc: Valley of the Sun (Arizona, US...
Actually since he is looking at an EOF error here would it not be better to do it this way

If @Error > 0 Exit
Else ? ...
EndIF

Top
#166458 - 2006-08-25 11:49 PM Re: Script error: array reference out of bounds!
ledzepe Offline
Fresh Scripter

Registered: 2005-12-06
Posts: 26
I added the line "? @error" after the split line and never got a value > 0. When I tried to redirectoutput to file there was a blank line at the top. Is that normal?
Top
#166459 - 2006-08-26 12:17 AM Re: Script error: array reference out of bounds!
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11631
Loc: CA
Yes, you're telling it to print a line by using ?

Here is some untested code to remove blank but still should do similar as Rad has already shown you.

Code:
WHILE @ERROR = 0
$Line = Split(readline(1),",")
If $Line
$Line[0] + $Line[3] + $Line[19] + $Line[21] ?
EndIf
LOOP



This would get rid of a blank but would still error if one of the elements of your array was empty
because you're telling it to use or show something that does not exist which generates the error.

Top
#166460 - 2006-08-26 02:05 AM Re: Script error: array reference out of bounds!
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
I don’t think it is necessary to define $Line as an array because I think the ‘Split()’ function does that for you every time it is used.

Code:

Dim $Line,$Element,$Output
$Output = ""
If Open(1,"p.csv",2) = 0
While @ERROR = 0
$Line = Split(ReadLine(1),",")
If $Line
For Each $Element in $Line
$Output = $Output + $Element
Next
? $Output
EndIf
Loop
EndIf

_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
#166461 - 2006-08-26 02:42 PM Re: Script error: array reference out of bounds!
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
benny your code doesn't actually do what he is trying to do, but I decided to golf it anyways
Code:

Dim $Element,$Output
if 0 = Open(1,"p.csv")
do
For Each $Element in Split(ReadLine(1),",")
$Output = $Output + $Element
Next
? $Output
until @ERROR
EndIf


Top
#166462 - 2006-08-26 04:02 PM Re: Script error: array reference out of bounds!
Benny69 Offline
Moderator
*****

Registered: 2003-10-29
Posts: 1036
Loc: Lincoln, Ne
lonk you da man
_________________________
Wait don't order yet,... get KiXforms Designer .NET 2.0 (Beta)
KiXforms Designer .NET 2.0 (Beta)

Top
Page 1 of 1 1


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

Who's Online
0 registered and 764 anonymous users online.
Newest Members
ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder, M_Moore
17887 Registered Users

Generated in 0.068 seconds in which 0.029 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