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
}