how to get a simple matrix (ArrayOfArrays) working ??

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Albert Schenning
Posts: 23
Joined: 03 Jan 2022, 14:40

how to get a simple matrix (ArrayOfArrays) working ??

Post by Albert Schenning » 13 Aug 2022, 01:17

I hope somebody can get me on the rigth track with the following script

Code: Select all

#Requires Autohotkey v2.0-beta.1+
#SingleInstance Force ; only run 1 instance of this script

; See it as an Excel sheet where A1 = 11, A2 = 12, A3=13,...etc, B1=21, B2 = 22... etc
; 11 12 13 14 15 ; these values could ofcourse be any other values but for simplicity purposes we will use these
; 21 22 23 24 25
; 31 32 33 34 35


colum := [""]
row := [""]
matrix := [colum, row]

; how to fill the values ?
matrix[1,1] := 11
matrix[1,2] := 12
;etc
matrix [3,3] = 33


; how to display the values

MsgBox matrix[1,1] ; trying to display 11
MsgBox matrix [3,3] ; trying to display 33
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 13 Aug 2022, 04:43, edited 2 times in total.
Reason: Topic moved from v2 'Scripts and Functions'. Please use code tags. Thank you!

DaveT1
Posts: 218
Joined: 07 Oct 2014, 11:23

Re: how to get a simple matrix (ArrayOfArrays) working ??

Post by DaveT1 » 13 Aug 2022, 03:24

Hey @Albert Schenning did you delete your original topic? I just posted a reply there and it's completely gone! And I think that the message above was from another poster replying to your original?

Albert Schenning
Posts: 23
Joined: 03 Jan 2022, 14:40

Re: how to get a simple matrix (ArrayOfArrays) working ??

Post by Albert Schenning » 13 Aug 2022, 03:29

I just deleted the original a minute ago because i think this is simpler to understand my question about the topic.

AHK_user
Posts: 515
Joined: 04 Dec 2015, 14:52
Location: Belgium

Re: how to get a simple matrix (ArrayOfArrays) working ??

Post by AHK_user » 13 Aug 2022, 07:12

3 solutions:

1. Accept syntax like Matrix[1][1] to use the standard AHK v2 syntax
https://lexikos.github.io/v2/docs/Objects.htm#Usage_Arrays_of_Arrays

2. Helgef created a array_n class to accept a lot of dimensions.
https://github.com/HelgeffegleH/AHK-misc./tree/master/classes/array_n

3. Before I found it, I had already created a own class :lol: , more based on a 2D array approach.
I included some very usefull methods and properties focused on a 2D approach.
Like retrieving all the values as a table (property [value]), ...
It also autofills non existing positions if you want to set a position that does not exist yet, something that the other ones do not do, it depends how you want to use it. This gives a lot more flexibility.

Code: Select all

#SingleInstance Force
#Requires AutoHotkey v2.0-beta.1

class Array2D {
    __new(Table:="") {
        this.value := Table
    }
    ; to use a syntax like Array2D[1,3] instead of Array2D[1][3]
    __Item[x, y] {
        get => ( this.array[y][x])
        set {
            if (this.array.length < y){
                loop y- this.array.length
                {
                    this.array.Push(Array())
                }
            }
            if (this.array[y].length < x) {
                loop y - this.array[y].length
                {
                    this.array[y].Push("")
                }
            }
            this.array[y][x] := value
        }
    }

    rows {
        get => this.array.length
    }

    columns {
        get {
            width := 0
            for key, arrayRow in this.array
            {
                width:= Max(width,arrayRow.Length)
            }
            return width
        }
    }

    ; returns the content of the array in a tab separated way
    value {
        get {
            result :=""
            for index, arrayRow in this.array
            {
                result .= index = 1 ? "" : "`n"
                for index, Value in arrayRow
                {
                    result .= index =1 ? Value : "`t" Value
                }
            }
            return result
        }
        set {
            this.array := Array()
            Loop parse, value, "`n", "`r"
            {
                y := A_Index
                this.array.Push(Array())
                Loop parse, A_LoopField, A_Tab
                {
                    this.array[y].Push(A_LoopField)
                }
            }
        }
    }

    ; get the 1d array of the row
    getRow(index_Row) {
        return this.array[index_Row]
    }

    ; get the 1d array of the colun
    getColumn(index_Column) {
        arrayColumn := Array()
        for index, arrayRow in this.array
        {
            arrayColumn.Push(arrayRow[index_Column])
        }
        return arrayColumn
    }

    removeRow(index_Row){
        this.array.RemoveAt(index_Row)
    }

    removeColumn(index_Column){
        for index, arrayRow in this.array
        {
            try arrayRow.RemoveAt(index_Column)
        }
    }
}

Data := "
(
21`t45`tds
66`t44
)"

grid := Array2D(Data)
; grid := Array2D(2,2) ; creating an empty table

grid[4, 4] := "5456"
gridtext := ""
; grid.removeColumn(2)
; grid.removeRow(2)
MsgBox grid.value
MsgBox grid.rows
MsgBox grid[2,1]

Albert Schenning
Posts: 23
Joined: 03 Jan 2022, 14:40

Re: how to get a simple matrix (ArrayOfArrays) working ??

Post by Albert Schenning » 13 Aug 2022, 10:51

thx very much, all i needed to know probably, I will experiment a bit with the suggested solutions.

DaveT1
Posts: 218
Joined: 07 Oct 2014, 11:23

Re: how to get a simple matrix (ArrayOfArrays) working ??

Post by DaveT1 » 14 Aug 2022, 03:22

Albert Schenning wrote:
13 Aug 2022, 03:29
I just deleted the original a minute ago because i think this is simpler to understand my question about the topic.
Hmm, please don't delete like that, especially as I had spent some time working through your problem for you and had already completed a post nailing the issue!

From what I can recall of my now lost post, I think that this will help:

Code: Select all

arrayA := ["a", "b"]
arrayB := ["Y", "Z"]
ArrayOfArrays := [arrayA, arrayB]

; how to display the values   ?
MsgBox "a " . ArrayOfArrays[1][1]  ; displays  "a a"
MsgBox("a " . ArrayOfArrays[1][2])  ; display  "a b" ;I prefer the 'function' format, but it's your choice obvs.
MsgBox ArrayOfArrays[2][1]  ; display  Y
MsgBox ArrayOfArrays[2][2]  ; display Z

Albert Schenning
Posts: 23
Joined: 03 Jan 2022, 14:40

Re: how to get a simple matrix (ArrayOfArrays) working ??

Post by Albert Schenning » 14 Aug 2022, 16:36

Updated the script of AHK_user with lots of MsgBoxes for us newbies.

Code: Select all

#SingleInstance Force
#Requires AutoHotkey v2.0-beta.1

class Array2D
 {
    __new(Table:="") {
    this.value := Table
    }
    ; to use a syntax like Array2D[1,3] instead of Array2D[1][3]
    __Item[x, y] {
        get => ( this.array[y][x])
        set {
            if (this.array.length < y){
                loop y- this.array.length
                {
                    this.array.Push(Array())
                }
            }
            if (this.array[y].length < x) {
                loop y - this.array[y].length
                {
                    this.array[y].Push("")
                }
            }
            this.array[y][x] := value
        }
    }

    rows {
        get => this.array.length
    }

    columns {
        get {
            width := 0
            for key, arrayRow in this.array
            {
                width:= Max(width,arrayRow.Length)
            }
            return width
        }
    }

    ; returns the content of the array in a tab separated way
    value {
        get {
            result :=""
            for index, arrayRow in this.array
            {
                result .= index = 1 ? "" : "`n"
                for index, Value in arrayRow
                {
                    result .= index =1 ? Value : "`t" Value
                }
            }
            return result
        }
        set {
            this.array := Array()
            Loop parse, value, "`n", "`r"
            {
                y := A_Index
                this.array.Push(Array())
                Loop parse, A_LoopField, A_Tab
                {
                    this.array[y].Push(A_LoopField)
                }
            }
        }
    }

    ; get the 1d array of the row
    getRow(index_Row) {
        return this.array[index_Row]
    }

    ; get the 1d array of the colun
    getColumn(index_Column) {
        arrayColumn := Array()
        for index, arrayRow in this.array
        {
            arrayColumn.Push(arrayRow[index_Column])
        }
        return arrayColumn
    }

    removeRow(index_Row){
        this.array.RemoveAt(index_Row)
    }

    removeColumn(index_Column){
        for index, arrayRow in this.array
        {
            try arrayRow.RemoveAt(index_Column)
        }
    }
}

Data := " "   ; just gets grid[1,1] a spaces  rest is not filled
grid := Array2D(Data)

if grid[1,1] = " "
    MsgBox "A   "  "grid[1,1]=   " grid[1,1]           "   grid[1,1] is just a space"
grid[100,100] := "100 by 100"
MsgBox  "B   " "grid [100,100] contains the text : " grid [100,100] "`n grid[99,99] would create an error because it doesnt exist"


grid[1,1] := "XX"   ; sets grid[1,1] to XX
MsgBox "C   " "grid[1,1]   " grid[1,1]  "`n inserted a new value in grid[1,1]"


; now filling some new data, this overwrites the old data
Data :="
(
A1`tB1`tC1
A2`t`tC2
`tB3`tC3
)"
grid := Array2D(Data) 

;msgbox grid[4,1]
MsgBox " MsgBox grid[1,4] or [4,1] creates an error here because it does not exist"


MsgBox  "D   " "grid := Array2D(Data) overwrites all the Data so grid[100,100] doesnt exist anymore this means refering now to grid[100,100] creates an error"
;msgbox grid[100,100]

MsgBox  "E   " "grid[1,1]= " grid[1,1]  " grid[2,1]= " grid[2,1] "  grid[3,1]= " grid[3,1]
MsgBox  "F   " "grid[1,1]= " grid[1,1]  " grid[1,2]= " grid[1,2] "  grid[1,3]= " grid[1,3] " `ngrid[1,3] exist but has no value (so not Space or Null) just nothing"


; testing   grid.removeColumn  &  grid.removeRow

MsgBox "G   " "grid.value :  " "`n" grid.value 
MsgBox "H   " "grid[1,1] =  grid[2,1] =    grid[3,1] =" " `n          " grid[1,1] "          " grid[2,1] "              " grid[3,1]
MsgBox "I   " "grid[1,1] = " grid[1,1]  "`n    grid[1,2] = " grid[1,2] " `n    grid[1,3] = " grid[1,3]
MsgBox "J   " "before  grid.removeColumn " "grid.rows =: " grid.rows "  grid.columns = " grid.columns


removing_columns:
MsgBox "K   " "before grid.removeColumn(2) :  " "`n" grid.value 
grid.removeColumn(2)
MsgBox  "M   " "after  grid.removeColumn(2) " "grid.rows = " grid.rows "  grid.columns = " grid.columns
MsgBox  "O   " "after  grid.removeColumn(2)  grid.value :  " "`n" grid.value  " `n    grid.removeColumn(2) wil shift column(3) to column(2) this means [3,1] & [3,2] & [3,3] dont exist anymore"


grid.removeRow(1)
MsgBox  "P   " "after  grid.removerow(1) " "grid.rows = " grid.rows "  grid.columns = " grid.columns
MsgBox  "R   " "after  grid.removerow(1)  grid.value :  " "`n" grid.value " `n grid.removerow(1) wil shift row(2) to row(1) & row(3) to row(2) this means [3,2] doesnt exist anymore"


^x::
    { ExitApp
    }
[Mod edit: [code][/code] tags added.]

gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: how to get a simple matrix (ArrayOfArrays) working ??

Post by gregster » 14 Aug 2022, 16:41

@Albert Schenning, please start putting code tags around your posted code. They will create those fancy code boxes which make code reading and copying much simpler. Please support your helpers by using those tags:
[code]code goes here[/code]

In the full editor there is even a button which you can use to insert them (fifth button from the left - first select the code, then press the button).
If you have any questions about this, please feel free to ask.
Thank you!

CraigM
Posts: 17
Joined: 21 Sep 2017, 13:07

Re: how to get a simple matrix (ArrayOfArrays) working ??

Post by CraigM » 15 Aug 2022, 07:38

Thank you, this is extremely helpful to me!

Post Reply

Return to “Ask for Help (v2)”