#207022 - 2013-04-02 08:55 AM
Array reference out of bounds error
|
Danks
Just in Town
Registered: 2013-03-27
Posts: 2
Loc: DK
|
Hello I have the following problem. I am getting the following error "array refrence out of bounse" and I can not quite figure out the cause. Does anyone has a suggestion on how to solve the problem.
The error occurs in the following code in the line( $Groups[$Count] = $Group - marked with red):
PS. I'm not a big KIX script guru ;-)
If (GetFileSize($ULTmpFile + ".iso8859") > 0)
$Err = Open(9,$ULTmpFile + ".iso8859")
$Readln = Readline(9) While (@Error = 0) If(InStr($Readln,"cn:")) $Group = SubStr($Readln, 5,Len($Readln)-4) ; ?"$Group $Count" GET$ $Groups[$Count] = $Group $Count = $Count + 1 EndIf $Readln = Readline(9) Loop
$ULCount = $Count - 1 ; Antallet af Grupper, startende ved 0 ; ?"$UlCount" $ULResult = 0 $Err = Close(9) Else $ULResult = 1 EndIf
|
Top
|
|
|
|
#207056 - 2013-04-04 04:06 PM
Re: Array reference out of bounds error
[Re: Lonkero]
|
Danks
Just in Town
Registered: 2013-03-27
Posts: 2
Loc: DK
|
Thank you for your response. It teased a bit because I did not originally written the code. ;-) Found out that the size of the array was underestimated and overlooked that it was placed further up in code .....
Is there any reason why Gleen argue that an array must not exceed 500 I can see it's possibil to dimension a array higher .....?
|
Top
|
|
|
|
#207059 - 2013-04-04 07:04 PM
Re: Array reference out of bounds error
[Re: Allen]
|
ChristopheM
Hey THIS is FUN
Registered: 2002-05-13
Posts: 309
Loc: STRASBOURG, France
|
I use an other solution to load file because resizing array at each line can be expensive in time and memory.
function LoadArrayFromFile( $filename, optional $PtrMax, optional $PtrInc )
if vartype($PtrMax)=0 $PtrMax = 1024 endif ; initialize max size
if vartype($PtrInc)=0 $PtrInc = 256 endif ; initialize increment size
dim $Ptr $Ptr = -1 ; set pointer to negative first
dim $handle $handle = freefilehandle()
if open( $handle, $filename )=0
redim $LoadArrayFromFile[$PtrMax] ; declare array var with a predefined size
dim $line
; Time to add an element to the array, possibly in a loop
$Line = ReadLine($handle)
While Not @ERROR
$Ptr = $Ptr + 1 ; increase pointer
if $Ptr > $PtrMax
$PtrMax = $PtrMax + $PtrInc ; increase array max size
ReDim Preserve $LoadArrayFromFile[$PtrMax] ; resize array
endif
$LoadArrayFromFile[$Ptr] = $Line ; put data in array
$Line = ReadLine($handle) ; read the next data line
Loop
$ = close($handle)
endif
if $Ptr=-1
; nothing read in the file or file not opened
$LoadArrayFromFile = ""
exit 1
else
ReDim Preserve $LoadArrayFromFile[$Ptr]
exit 0
endif
endfunction This is a function that allows to define initial size of the array and increment size when array becomes too small to load next data.
See below how to test with differents sizes of buffer and increment$=SetOption( "Explicit", "ON" )
dim $filename, $arr, $start
$filename = "<name of a large file for test>"
$start=@TICKS
$arr = LoadArrayFromFile( $filename, 0, 1 )
$start=@TICKS-$start
"Loaded line by line ( 0, 1) in " $start " ms" ?
$start=@TICKS
$arr = LoadArrayFromFile( $filename )
$start=@TICKS-$start
"Loaded with default (1024, 512) in " $start " ms" ?
$start=@TICKS
$arr = LoadArrayFromFile( $filename, 4096, 4096 )
$start=@TICKS-$start
"Loaded with buffer (4096,4096) in " $start " ms" ?
with a file of 32000 lines (1,1MB), here are the results :
Loaded line by line ( 0, 1) in 469 ms
Loaded with default ( 1024, 512) in 312 ms
Loaded with default ( 4096,4096) in 297 ms
_________________________
Christophe
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 425 anonymous users online.
|
|
|