Jump to content

ReDim and Array Dimension syntax


Go to solution Solved by genius257,

Recommended Posts

Good day AutoIt Community;

I have a question regarding changing an Array’s dimension, using ReDim. I have come across a curiosity, and wondered “why”? Perhaps it is some common knowledge thing I have missed?

The question is: Why can I not ReDim an Array from a 2D array to a 1D array and use corresponding syntax? i.e:

Local $aTest[1][2]


ReDim $aTest[1][1]


$aTest[0] = 10 ; Fails -- Array variable has incorrect number of subscripts or subscript dimension range exceeded.


$aTest[0][0] = 10 ; Works

 I am trying to make a function where the user has the option to add a column, if they decide not to, the resulting array would be a single column. Thus the dimension would be a variable instead of a integer, as the above example is. Is my method the only problem? And instead to make a single column I need to do it like this:

ReDim $aTest[1]

Thanks for any clarification.

 

LibreOffice UDF  ; Scite4AutoIt Spell-Checker Using LibreOffice

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

 

Link to comment
Share on other sites

It has nothing to do with ReDim, it fails because you have a multidimensional array so when you assign a value you must specify the dimensions. If I understand well what you need, you can have a 1D array and the value assigned for each index should be a simple value or an array based on the option selected by the user. Something like that.

Local $aTest[10]

Local $Options[2]

$aTest[0] = 0
$aTest[1] = $Options
...
Link to comment
Share on other sites

  • Solution

Hi @donnyh13

Array sizes are fixed to the number of dimensions you need between 1 and 64.

Each dimension in the array is represented via one pair of square brackets.

; One dimension
Global $a[1]
;Two dimensions
Global $a[1][1]
;Three dimensions
Global $a[1][1][1]

So your current ReDim expression changes the size the the size of that dimension, but not the number of dimensions.

34 minutes ago, donnyh13 said:

And instead to make a single column I need to do it like this:

ReDim $aTest[1]

Yes that would reduce your 2D array to a 1D array.

 

I am not entirely sure i understand what your function should do, but if you only need to add a column to the array, maybe something like this:

#include <Array.au3>

Global $aTest[1][2]
$aTest[0][0] = 10
$aTest[0][1] = 20

_ArrayDisplay($aTest)
MyFunction($aTest, 25, True)
_ArrayDisplay($aTest)
MyFunction($aTest, 30, False)
_ArrayDisplay($aTest)

Func MyFunction(ByRef $array, $value, $addColumn = False)
    If $addColumn Then Redim $array[1][UBound($array, 2) + 1]
    $array[0][UBound($array, 2) - 1] = $value
EndFunc

 

Link to comment
Share on other sites

  

Alright, thanks guys. The problem clearly stems from my lack of an understanding of dimensional arrays. I was previously understanding that $aArray[1][2] meant 1 row 2 columns/dimensions; and thus $aArray[1][1] would mean 1 column/dimension. But I now see dimensions is not the same as columns 😳.  Which should have been obvious, had I read the help file slowly.

Thank you for the examples, and help. As for my goal, what I am working with is obtaining Objects for Shapes contained in a LibreOffice document. Currently column 0 contains the object, and then optionally the other columns can contains an integer corresponding to some constants, and also the implementation name, helping to identify what shape the Object is for, and so on for each row. Thus if the user only wants the Objects, it would only a 1D array of Objects. I was attempting to limit the number of If/Else's. But I think I have a better grasp of how to solve it now.

LibreOffice UDF  ; Scite4AutoIt Spell-Checker Using LibreOffice

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

 

Link to comment
Share on other sites

Okay, thanks for that clarification @Andreik

LibreOffice UDF  ; Scite4AutoIt Spell-Checker Using LibreOffice

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

 

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