thepip3r
(Hey THIS is FUN)
2006-02-03 12:15 AM
Problem with @ERROR output -- yielding funny results...

The code below is a snippet of a larger bit of code that runs fine; it's the script below I'm having problems with...

Edit: Added $vars for the conditional in question...

Code:
$OpenReorgFile  = Open(3,$ReorgFile,2)
$PhoneDirFile = @ScriptDir + '\Balad_Telephone_Book.csv'
$OpenPDFile = Open(4,$PhoneDirFile,2)
$OutputFile = @ScriptDir + '\Morale_Call_Output.csv'
$OpenOutputFile = Open(5,$OutputFile,5)

If $OpenReorgFile = 0 AND $OpenPDFile = 0 AND $OpenOutputFile = 0
$rLine = ReadLine(3)
Do
$rArrLine = Split($rLine,',')
$pdLine = ReadLine(4)
Do
$pdArrLine = Split($pdLine,',')
? $pdArrLine[4]

$pdLine = ReadLine(4)
Until @ERROR <> 0
$rLine = ReadLine(3)
Until @ERROR <> 0
Else
? 'Error: ' + @ERROR + ' -- ' + @SERROR
Endif



I don't know why but the very first conditional is working because the files are getting created and stuff but my ELSE code is executing too... BUT it's executing as "Error: 0 -- The operation completed successfully." Does that make sense?

Edit2: Interestingly enough, when I comment my whole second Do loop, it runs fine echoing the first array... if that helps at all.


Les
(KiX Master)
2006-02-03 01:18 AM
Re: Problem with @ERROR output -- yielding funny results...

That second inner Do loop makes no sense to me. What is it supposed to loop on? I see no reason for it to loop.

thepip3r
(Hey THIS is FUN)
2006-02-03 02:09 AM
Re: Problem with @ERROR output -- yielding funny results...

Well the inner DO loop currently ISN'T doing anything but it will be. When I write code, I tend to have so many errors that at every little step, I stop, save it, and run it to make sure that I don't get 20+ lines deep in code and have a small screwup somewhere near the beginning. I've actually narrowed it down to where it's erroring out saying that there's an array reference out of bounds. The funny thing is, it runs through about 25 or so iterations before it errors out...

thepip3r
(Hey THIS is FUN)
2006-02-03 03:15 AM
Re: Problem with @ERROR output -- yielding funny results...

Anyone got any pointers on what an "array reference out of bounds" usually means?

thepip3r
(Hey THIS is FUN)
2006-02-03 03:33 AM
Re: Problem with @ERROR output -- yielding funny results...

Ok, some further progress... when I do some error trapping, I actually found in the loop where the error is occurring and the following error is reported by @ERROR/@SERROR:

"Error: -1 -- Error(317 / 13D) while retrieving error information for FFFFFFFF"

...then that's when it gives the KiX beep and says: "array reference out of bounds"

If it helps anyone to see where the error gets trapped:

Code:
$OpenReorgFile  = Open(3,$ReorgFile,2)
$PhoneDirFile = @ScriptDir + '\Balad_Telephone_Book.csv'
$OpenPDFile = Open(4,$PhoneDirFile,2)
$OutputFile = @ScriptDir + '\Morale_Call_Output.csv'
$OpenOutputFile = Open(5,$OutputFile,5)

If $OpenReorgFile = 0 AND $OpenOutputFile = 0 AND $OpenPDFile = 0
$rLine = ReadLine(3)
Do
$rArrLine = Split($rLine,',')
? '1: ' + $rArrLine[4]
$pdLine = ReadLine(4)
If @ERROR <> 0
? 'Error: ' + @ERROR + ' -- ' + @SERROR
Endif
sleep 5
Do
$pdArrLine = Split($pdLine,',')
? '2: ' + $pdArrLine[4]
$pdLine = ReadLine(4)
Until @ERROR <> 0
$rLine = ReadLine(3)
Until @ERROR <> 0
Else
? 'Error: ' + @ERROR + ' -- ' + @SERROR
Endif



If grabs the first variable in the first loop, runs it against the whole file in the second loop, grabs the second variable for the first loop but when it goes for the first ReadLine() in the second instance of the second loop is when I get the error.

Anyone have any ideas?


Richard H.Administrator
(KiX Supporter)
2006-02-03 10:43 AM
Re: Problem with @ERROR output -- yielding funny results...

Add some code to check that your file opens have worked. Don't ever assume that anything will work, because at some point it won't.

Make sure that you are not trying to read past the end of any of your files.

If you are going to split a line you need to check the size of the array after the split. If you read a blank line, or a line which has less fields than you expect then there is a chance that the array is not as big as you were expecting. When you attempt to reference a cell past the end of the array you will get an "out of bounds" error.

If you don't want to check the size each time and the lines are of varying lengths then a trick I use is to add enough blank elements to the end of the line to ensure that the array reference will never be out of bounds.

For example the "+',,,,'" in the following code ensures that there is always at least 5 elements in the array, so that the next line can never cause an out-of-bounds error.
Quote:

$rArrLine = Split($rLine+',,,,',',')
? '1: ' + $rArrLine[4]




thepip3r
(Hey THIS is FUN)
2006-02-03 11:11 PM
Re: Problem with @ERROR output -- yielding funny results...

Richard: Isn't my If statement supposed to check to see if it opened correctly? I thought that's what I was doing with my IF/Else. I'll go ahead and try to check the size but I still don't understand what's going on because it's the ReadLine() function that's returning the error and it's not an array at that point...

thepip3r
(Hey THIS is FUN)
2006-02-03 11:38 PM
Re: Problem with @ERROR output -- yielding funny results...

Thanx for the help Richard. I wrote those ',,,,' to my split and I'm not getting the error anymore. It's hard to go from an easy language like PHP to scripting language where you have to do more things by yourself.

I'm still having problems with my readline but I think I just got dropped on my head or something because now that i know the error is -1 on my readline() function, i know it has something to do with maybe only being able to loop through the file once without closing and reopening it again or something...


thepip3r
(Hey THIS is FUN)
2006-02-04 12:07 AM
Re: Problem with @ERROR output -- yielding funny results...

Yes that seems to be my problem and no matter where I call Open() from on that file, I still get that message. Can anyone shed some light on why that's happening? Why on the inner-loop is the file that I open only able to be parsed 1 time before my ReadLine(4) on teh second go around of the loop returns a -1 (EOF Error)????

All help would be appreciated.


thepip3r
(Hey THIS is FUN)
2006-02-04 12:32 AM
Re: Problem with @ERROR output -- yielding funny results...

Ok, that might not be my problem then because when I run the script where I close the inner file everytime on the loop, I get a file handle error. If I was to use the freefilehandle() function, how would I be sure that I got the file handle for the file handle in question? Am I even trying to open/close the file the correct way?

Code:
$OpenReorgFile  = Open(3,$ReorgFile,2)
$PhoneDirFile = @ScriptDir + '\Balad_Telephone_Book.csv'
$OpenPDFile = Open(4,$PhoneDirFile,2)
$OutputFile = @ScriptDir + '\Morale_Call_Output.csv'
$OpenOutputFile = Open(5,$OutputFile,5)

If $OpenReorgFile = 0 AND $OpenOutputFile = 0
$rLine = ReadLine(3)
Do
$rArrLine = Split($rLine+',,,,',',')
? '1: ' + $rArrLine[4]
If $OpenPDFile = 0
$pdLine = ReadLine(4)
If @ERROR <> 0
? 'Error: ' + @ERROR + ' -- ' + @SERROR
Endif
sleep 5
While @ERROR = 0
$pdArrLine = Split($pdLine+',,,,',',')
? '2: ' + $pdArrLine[4]
$pdLine = ReadLine(4)
Loop
$ = Close(4)
Endif
$rLine = ReadLine(3)
Until @ERROR <> 0
Else
? 'Error: ' + @ERROR + ' -- ' + @SERROR
Endif

$ = Close(3)
$ = Close(5)



thepip3r
(Hey THIS is FUN)
2006-02-04 12:39 AM
Re: Problem with @ERROR output -- yielding funny results...

well this didn't work:

Code:
$OpenReorgFile  = Open(3,$ReorgFile,2)
$PhoneDirFile = @ScriptDir + '\Balad_Telephone_Book.csv'
$OutputFile = @ScriptDir + '\Morale_Call_Output.csv'
$OpenOutputFile = Open(5,$OutputFile,5)

If $OpenReorgFile = 0 AND $OpenOutputFile = 0
$rLine = ReadLine(3)
Do
$rArrLine = Split($rLine+',,,,',',')
? '1: ' + $rArrLine[4]
$handle = FreeFileHandle()
If Open($handle,$PhoneDirFile,2) = 0
$pdLine = ReadLine(4)
If @ERROR <> 0
? 'Error: ' + @ERROR + ' -- ' + @SERROR
Endif
sleep 5
While @ERROR = 0
$pdArrLine = Split($pdLine+',,,,',',')
? '2: ' + $pdArrLine[4]
$pdLine = ReadLine(4)
Loop
$ = Close(4)
Endif
$rLine = ReadLine(3)
Until @ERROR <> 0
Else
? 'Error: ' + @ERROR + ' -- ' + @SERROR
Endif

$ = Close(3)
$ = Close(5)



...this code won't even allow me to parse the file the first time...


thepip3r
(Hey THIS is FUN)
2006-02-04 12:48 AM
Re: Problem with @ERROR output -- yielding funny results...

WOOT!!! Fixed it so far! In the script I posted above, I'm using freefilehandle() but didn't change the static calls in ReadLine() or Close(); changing them to $handle worked great! Sorry for the extraneous post! Thanx for the help as always guys!