Jump to content

[SOLVED] Dynamically resize a 2d array


Recommended Posts

I need to dynamically resize my 2d array while looping. 

I know this code:

ReDim $rArray[UBound($rArray) + 1]

works for the rows, however, I also need to increase the columns. How would i go about increasing both rows and columns while looping? 

Edited by nooneclose
Solved
Link to comment
Share on other sites

  • Moderators

nooneclose,

ReDim is a horribly slow function and should be used as sparingly as possible - certainly never in a loop. I suggest that you create the array at a sensible size initially and then only increase it when it gets full. See the final example and paragraph in the Recursion tutorial in the Wiki to see what I mean.

As to your specific question, you use very similar syntax to increase the columns:

ReDim $rArray[UBound($rArray) + 1][UBound($rArray, 2) + 1]

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

23 minutes ago, nooneclose said:

@Melba23 Thank you for knowledge. I didn't know that ReDim could cause performance issues. the 2d Array i am creating should never be bigger than 20 rows and 20 columns. Should i just make an array [20][20] ? 

That seems to be what @Melba23 is suggesting.  You can then check to see elements in the array are not used while looping through it using something like

If StringIsSpace ( $array[1][1] ) Or $array[1][1] = Null Then

 

Link to comment
Share on other sites

  • Moderators

nooneclose,

Quote

Should i just make an array [20][20] ?

With such a small array I would suggest that is the most sensible solution. You will need to keep track of the rows and/or columns that you fill so as to correctly insert the next data point and you could easily use those variables to determine which elements actually hold data. If you can produce some example code i would be happy to look at it and make any suggestions that spring to mind.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

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

×
×
  • Create New...