Jump to content

How to add dimension to an existing array?


zwierzak
 Share

Recommended Posts

Hello. I have a problem. I've described it in the code's comments

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global $item[30]
GUICreate("listview items", 220, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
GUISetBkColor(0x00E0FFFF) ; will change background color
$listview = GUICtrlCreateListView("X|Y", 10, 10, 165, 200)
$button = GUICtrlCreateButton("Refresh", 5, 220, 70, 20)
$button2 = GUICtrlCreateButton("test", 75, 220, 70, 20)
GUISetState()

While 1
    $msg = GUIGetMsg()
    Switch $msg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $button
            $var = WinList("notepad") 
            For $i = 1 To $var[0][0]
                
                If $var[$i][0] <> "" And IsVisible($var[$i][1]) Then 
                    $string = StringSplit($var[$i][0], "-")
                    $item[$i] = GUICtrlCreateListViewItem($string[3], $listview)
                    
                    ;HOW TO MAKE IT LIKE THIS?
                    ;$string[$i] = StringSplit($var[$i][0], "-")
                    ;$item[$i] = GUICtrlCreateListViewItem($string[$i][3], $listview)
                    ;I want to make X number of strings, but it already has got 1 array. How to make another one - add new dimension to the existing array?
                EndIf
            Next
        Case $button2
            MsgBox(0,GUICtrlRead($listview), "text")
    EndSwitch
WEnd

Func IsVisible($handle)
    If BitAND(WinGetState($handle), 1) Then
        Return 1
    Else
        Return 0
    EndIf

EndFunc   ;==>IsVisible

I'd like to make it like this but dunno how

;HOW TO MAKE IT LIKE THIS?

;$string[$i] = StringSplit($var[$i][0], "-")

;$item[$i] = GUICtrlCreateListViewItem($string[$i][3], $listview)

;I want to make X number of strings, but it already has got 1 array. How to make another one - add new dimension to the existing array?

Edited by zwierzak
Link to comment
Share on other sites

"How to add new dimension to the existing array?"

Assuming: while maintaining the data inside the array.

By copying the data from one array(old) to the other array(new)

Redim allows you to change the size of the exiting dimensions while maintaining the data inside the array.

(Redim also allows the number of dimensions of a existing array to be changed, but in such a case the array data will be lost/cleared.)

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

  • Moderators

zwierzak,

Just use ReDim like this: ;)

#include <Array.au3>

Global $aArray[2][2] = [["A", "B"], ["C", "D"]]

_ArrayDisplay($aArray)

ReDim $aArray[UBound($aArray)][3]

_ArrayDisplay($aArray)

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

@Melba23: As far as I remember this wasn't possible in older versions of AutoIt, but it seems the behaviour has changed. This means, MvGulik's statment is now incorrect. Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Thats weird for me, cos Redim didn't work for me. So I've tried to make another array which will keep the previous array's data. And u know what? It works xD

Global $newstring[30]
$string = StringSplit($var[$i][0], "-")                     
_ArrayDisplay($string)                  
$newstring[$i] = $string[3]                     
$item[$i] = GUICtrlCreateListViewItem($newstring[$i], $listview)
Link to comment
Share on other sites

  • Moderators

Guys,

Do not get confused between the number of dimensions (the number of [ ] after the array name) and the size of existing dimensions (changing the number within an existing [ ]). ;)

If you want to retain the existing data in an array, there is no way to increase the number of dimensions, you have to copy the data from the existing array to another array with the required number of dimensions. Using any form of declaration (Global/Local/Dim/Redim) and changing the number of dimensions will erase all existing elements from the array.

However, by using ReDim you can alter the size of existing dimensions without deleting existing elements as I showed above. :)

M23

Edit: Amended as explained in the posts below.

Edited by Melba23

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

Oh right. I really should read more carefully.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Guys,

ReDim will not increase the number of dimensions - it will not turn $aArray[x][y] into $aArray[x][y][z]. But it will alter the size of existing dimensions as I showed above. ;)

Do not get confused between the number of dimensions (the number of [ ] after the array name) and the size of existing dimensions (changing the number within an existing [ ]). ;)

M23

Oh dear, I've written a function specially to deal with my misreading of that. :)

D

Link to comment
Share on other sites

ReDim will not increase the number of dimensions - it will not turn $aArray[x][y] into $aArray[x][y][z]. But it will alter the size of existing dimensions as I showed above. ;)

Brrr. Why do I have a problem with this. O yea, being a nitpick. :)

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

  • Moderators

MvGulik,

What is bugging you about the statement? I am happy to change it if you feel it is less than clear. ;)

My definition of the number of dimensions matches that used in geometry:

1D array = a line of elements, with a single coordinate

2D array = a plane with x,y coordinates

3D array = a volume with x, y, z coordinates

......

The size of the dimensions just sets the length, area and volume respectively of the space needed to hold the elements. :)

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

Just taking a shot in the dark here Melba, but I'm guessing the nitpicking is about the fact that you can increase the number of dimensions with ReDim. The problem is doing that will destroy the data and has a net effect of being just another new 'Dim'.

MvGulik was correct in his first post regarding copying the data to a new multi-dimensional array, as that is the only way to 'add' dimensions.

Link to comment
Share on other sites

Nothing wrong with your definitions. You know them better than I do.

But...

Local $array[1]=[1]
    DebugOut('$array', $array) ;### Debug DebugOut.
    ReDim $array[1][2]
    DebugOut('$array', $array) ;### Debug DebugOut.
!> MAIN()
--- --- 0 (array dump), $array:[1].
.   000 |I3| 1 (0x00.00.00.00.00.00.00.01)
--- --- 0 [0,0]
--- --- 0 (array dump), , $array:[1][2].
--- |--| 0| 1|
[0] |St|  |  | 
--- --- 0 [0,0]
!< MAIN()

Redim just changed the number of dimensions. from array[x] to array[x][y]

(its the fact that you can't do it while preserving the data inside a array.)

---

Depending on the point of view. Your also correct of course. As it could be argued that when Redim is used to change the number of dimensions on a existing array. Redim will internally toss the existing array/data and create a new(empty) array for the used variable/$name.

;)

Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

  • Moderators

MvGulik & Ascend4nt,

Understood. :)

I must admit that I had not realised that ReDim could actually redeclare an array with a different number of dimensions - albeit loosing the data in the process. ;) I have only ever used it to resize existing dimensions - as the OP needed.

You live and learn each day - I have amended the earlier post. Thanks for pointing it out. ;)

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...