Sign in to follow this  
Followers 0
Dattebayo

Why doesnt this Array work properly?

6 posts in this topic

#1 ·  Posted (edited)

Dim $test[13][1]
Hello()

Func Hello()

    MsgBox(0,"",IsArray($test))
    For $x = 1 To Ubound($test)-1
    $test[$x] = "hmm" ; i want to write to the first dim.

Next
Endfunc

How come i declare a 2D array and then cant access its 1st dimension? This cant be right.

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

$test[$x] = "hmm"

^ ERROR

Edited by Dattebayo

Share this post


Link to post
Share on other sites



You have specified a second dimension so you need to use that when filling the array.

Why not simply use?:

Dim $test[13]

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

You have specified a second dimension so you need to use that when filling the array.

Why not simply use?:

Dim $test[13]

Jos

That code is just to demonstrate my problem. What I'm writing is a bigger project but Ive run into this issue. I'm writing a function to split a 1D array into a 2D array using a delimiter. However when i split the string then try to $newarray[1] = "firsthalf" then $newarray[1][1] = "Secondhalf" it errors out because of above problem.

Tried Redim earlier but doesn't work with 1D -> 2D as you know.

Edited by Dattebayo

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Fine, but you either use one or 2 dimensions.

Your example has 2 thus you need to use 2 when you address values in the array.

EDIT: Show an example where redim doesn't as you want.

Jos

Edited by Jos

Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Fine, but you either use one or 2 dimensions.

Your example has 2 thus you need to use 2 when you address values in the array.

EDIT: Show an example where redim doesn't as you want.

Jos

I understand. I meant Redim does not preserve values if for example I fill a 1dim array then redim it to 2dim array. It'll make it 2dim but the values are gone. I tried that first so the above problem i guess was my plan B. Now i gotta come up with Plan C. :) anyway thanks for help.

Share this post


Link to post
Share on other sites

#include <array.au3>
Dim $test[13][1]
Hello()
_ArrayDisplay($test)

Func Hello()

    MsgBox(0,"",IsArray($test))
    For $x = 1 To Ubound($test)-1
    $test[$x][color="#FF0000"][0][/color] = "hmm" ; i want to write to the first dim.

Next
Endfunc

Now the first collumn (collumn 0), except for $test[0][1], is full of "hmm".

Guess you are confusing Rows, Collumns and Dimensions. You want to access the first collumn, not the first Dimension.

Example:

$array[4][3] has 5 Rows, 4 Collumns, 2 Dimensions. You can access single Elements, or whole Collumns or Rows, but not Dimensions.

Even the Rows and Collumns are just a concept to picture a 2D Array.

Example for 5D Array:

$array[123][4][5][67][89]

You have 124 Houses, each House has 5 Rooms, in each Room there are 6 Cabinets, with 68 Drawers per Cabinet and 90 Filecards in each Drawer.

To access an information, you need to know which filecard, in which drawer, cabinet, room, house, so all 5 Dimensions.

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