Jump to content

I Feel Like A Dork For Asking But Here Goes.


Recommended Posts

Can anyone tell me what I'm doing wrong with this code? It's the first time I've ever tried to use an array in AutoIT. I need to pass an array into a function. The function needs to redim it according to whatever I need it to return. I've tried various code changes but I can't get it to compile without errors. The main procedure does not know how much is coming back so is it right to configure it as $mylines[1] if only to ensure it is an array?

I also thought arrays were 1-based but it gives me this error: C:\Temp\Test.au3 (9) : ==> Array variable has incorrect number of subscripts or subscript dimension range exceeded.:

Help appreciated.

dim $mylines[1]
TestArray($mylines)
Exit

func TestArray(byref $mylines)
    redim $mylines[3]
    $mylines[1] = "ABC"
    $mylines[2] = "DEF"
    $mylines[3] = "GHI"
    Return
EndFunc
Link to comment
Share on other sites

dim $mylines[1]
TestArray($mylines)
Exit

func TestArray(byref $mylines)
    redim $mylines[4]
    $mylines[0]; not being used
    $mylines[1] = "ABC"
    $mylines[2] = "DEF"
    $mylines[3] = "GHI"
    Return
EndFunc

Edited by gafrost

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

dim $mylines[1]
TestArray($mylines)
local $i
for $i = 0 to ubound($mylines) - 1
msgbox(0,"",$mylines[$i])
Next
Exit

func TestArray(byref $mylines)
    redim $mylines[4]
    $mylines[0] = "?????"; not being used
    $mylines[1] = "ABC"
    $mylines[2] = "DEF"
    $mylines[3] = "GHI"
    Return
EndFunc

In your reply you said that [0] not being used. I can assign to [0] so when your comment said not being used is that by convention?

Link to comment
Share on other sites

dim $mylines[1]
TestArray($mylines)
local $i
for $i = 0 to ubound($mylines) - 1
msgbox(0,"",$mylines[$i])
Next
Exit

func TestArray(byref $mylines)
    redim $mylines[4]
    $mylines[0] = "?????"; not being used
    $mylines[1] = "ABC"
    $mylines[2] = "DEF"
    $mylines[3] = "GHI"
    Return
EndFunc

In your reply you said that [0] not being used. I can assign to [0] so when your comment said not being used is that by convention?

no it was by what code you supplied

or I could have resassigned your orignal array i.e.

dim $mylines[1]
TestArray($mylines)
Exit

func TestArray(byref $mylines)
    redim $mylines[3]
    $mylines[0] = "ABC"
    $mylines[1] = "DEF"
    $mylines[2] = "GHI"
    Return
EndFunc

SciTE for AutoItDirections for Submitting Standard UDFs

 

Don't argue with an idiot; people watching may not be able to tell the difference.

 

Link to comment
Share on other sites

Incase you missed why that didn't work...

In your code you had redimmed the array to have 3 elements.

redim $mylines[3]

The array spots you made by doing that are:

$mylines[0]

$mylines[1]

$mylines[2]

When you tried to stick something into $mylines[3], autoit didn't know what you were talking about. Hense the error.

[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]

Link to comment
Share on other sites

Thanks guys. I understand that ok now. I see it's C-style, 0-based and not 1-based as I thought it was - for some daft reason that only a dork would assume! :)

The array now works as I was hoping it would.

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...