Sign in to follow this  
Followers 0
zbatev

Array variable has incorrect number of subscripts or subscript dimension range exceeded

5 posts in this topic

Hello again,

Can somebody please explain this error on this code?

Error: "Array variable has incorrect number of subscripts or subscript dimension range exceeded."

$str = "Sun,Mon,Tue,Wed,Thu,Fri,Sat"
$strarr = StringSplit($str,",",1)
$disp = ""
For $rd = 1 to UBound($strarr)
    $disp = $disp & $strarr[$rd] & @CRLF
Next
1 person likes this

Share this post


Link to post
Share on other sites



I think I can answer my own question, the Ubound function is returning an extra 1 element instead of 7(sunday to raturday) it returns 8.

Went to Help and found this:

Remarks

Remember that the value returned by UBound is one greater than the index of an array's last element!

Hmmmm :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Use Ubound this way in this case:

For $rd = 1 to UBound($strarr) - 1
Edited by BrewManNH

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

In autoit, and many other scripting languages, arrays typically include and begin with element 0.

So if you did:

$str = "one,two,three" 
$strarr = StringSplit($str,",",1)

Then:

$strarr[0] = "one"
$strarr[1] = "two"
$strarr[2] = "three"

ubound returns the number of elements in the array, so ubound($strarr) would = 3

$strarr[3] gives the error you see because the last element of the array is actually $strarr[2]

This is why you will so often see the code: "ubound($array) - 1" being used.


[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Share this post


Link to post
Share on other sites

In autoit, and many other scripting languages, arrays typically include and begin with element 0.

So if you did:

$str = "one,two,three" 
$strarr = StringSplit($str,",",1)

Then:

$strarr[0] = "one"
$strarr[1] = "two"
$strarr[2] = "three"

StringSplit with flag 1 return the count in the first element and not $strarr[0] = "one".

You would be right if flag was 2 ! Posted Image


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
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
Sign in to follow this  
Followers 0