Jump to content

Catching Array Out of Bounds Errors?


jmoskie
 Share

Recommended Posts

My script does some screen scraping from an application. It gets the text, and then it goes through it line by line.

The format is generally

User

Joe

San Jose

California

555-555-5555

If it finds a keyword string, such as "User", it knows something about the next few lines, as the format is standard.

When it's looking at $lines[$l] and finds "User", it knows that $lines[$l+1] is the user's name, $lines[$l+2] is the user's city, etc.

The problem I'm facing now is handling what happens when the application I'm screen scraping disappears. If you exit that program, it's possible that I get a partial screen scrape, such as:

User

Joe

Then, when I reference $lines[$l+2] to get the city, I get an array out of bounds exception.

I've started adding some bounds checking into the code, but I was hoping there would be a way to "catch" these and handle them properly. Is there?

Link to comment
Share on other sites

Do a UBound() check on the array before you start trying to access it.

If Ubound($lines) > $l+2 Then

;Do your accessing here

Else

;Do your error handling here

Endif

Where $l+2 is the greatest value that will be accessed in the array in the If Then statement.

- The Kandie Man ;-)

EDIT: Fixed comparison operator

Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

Hi,

check size of array:

e.g.:

If (UBound ($lines) - 1) < ($I + 4) Then MsgBox (0,"","Invalid array size!")

;-))

Stefan

P.S: You might post some code to get further help!

P.S.S: haha, double post.....

Edited by 99ojo
Link to comment
Share on other sites

$iUbound = Ubound($array) -1

Then in the loop you are using

If $i+1 <= $iUbound Then.... Whatever

If $i+2 <= $iUbound Then.... Whatever

Etc.

If you don't want any info entered if the last group is not complete then you can make it easier.

For $i = 1 To $ibound

If $i+4 > $iUbound Then ExitLoop

;Your regular code goes here

Next

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...