在Autohotkey中创建多维数组,该数组根据用户输入进行更新

发布于 2025-02-12 19:56:54 字数 1813 浏览 2 评论 0原文

Autohotkey中的一般问题

,我很难将值附加到array对象,该对象开始空,然后在用户执行某些操作时被值填充。

下面复制的脚本

是我脚本的小型可重复版本。在其中,目标是为numpadadd键,将当前鼠标位置添加到array nater 称为list_of_mouse_positions。添加几点后,用户可以使用numpad0键来查看迄今为止存储的位置列表。

;---------------;
; GENERAL SETUP ;
;---------------;

coordmode Mouse, screen

; Creates an empty array. This will store the mouse positions.
list_of_mouse_positions := Array()

;-----------;
; FUNCTIONS ;
;-----------;

; Function used to add the current mouse position to the global
; list of mouse positions. The `x` parameter serves no purpose.
AddLocation(x)
{
    MouseGetPos, xpos, ypos
    mouse_pos := Array()
    mouse_pos[1] := xpos
    mouse_pos[2] := ypos
    list_of_mouse_positions.Push(mouse_pos)
}

;-------------;
; KEYBINDINGS ;
;-------------;

; Adds the current mouse position to the 
NumpadAdd::
    AddLocation(0)
    return

; Checking the mouse positions stored so far
NumPad0::
    MsgBox, %list_of_mouse_positions%
    return

预期输出与实际输出

运行脚本并击中numpadadd键以捕获几个坐标,我点击了numpad0键。我期望出现的是一个包含所有坐标的消息框。但是,出现的只是一个空的消息框。

特定问题

如何通过将新的行附加到其上,每个行都会存储两个新值?

注意list_of_mouse_positions的预期维度,

我希望list_of_of_mouse_positions变量变量为多维数组(nx 2)所有x值,第二列包含所有y值。

例如,假设用户“捕获”以下鼠标位置的坐标:

  • (100,200)
  • (400,100)
  • (600,150)

在这种情况下,list_of_of_mouse_positions的结构将是如下:

list_of_mouse_positions[1,1] = 100
list_of_mouse_positions[1,2] = 200
list_of_mouse_positions[2,1] = 400
list_of_mouse_positions[2,2] = 100
list_of_mouse_positions[3,1] = 600
list_of_mouse_positions[3,2] = 150

General problem

In AutoHotKey, I'm having a hard time appending values to an Array object that starts off empty and then gets filled up with values as the user performs certain actions.

The script

Copied below is a small reproducible version of my script. In it, the goal is for the NumpadAdd key to add the current mouse position to the Array called list_of_mouse_positions. After adding a few points, the user can use the Numpad0 key to look at the list of positions stored thus far.

;---------------;
; GENERAL SETUP ;
;---------------;

coordmode Mouse, screen

; Creates an empty array. This will store the mouse positions.
list_of_mouse_positions := Array()

;-----------;
; FUNCTIONS ;
;-----------;

; Function used to add the current mouse position to the global
; list of mouse positions. The `x` parameter serves no purpose.
AddLocation(x)
{
    MouseGetPos, xpos, ypos
    mouse_pos := Array()
    mouse_pos[1] := xpos
    mouse_pos[2] := ypos
    list_of_mouse_positions.Push(mouse_pos)
}

;-------------;
; KEYBINDINGS ;
;-------------;

; Adds the current mouse position to the 
NumpadAdd::
    AddLocation(0)
    return

; Checking the mouse positions stored so far
NumPad0::
    MsgBox, %list_of_mouse_positions%
    return

Expected Output vs Actual Output

After running the script and hitting the NumpadAdd key to capture a couple of coordinates, I hit the Numpad0 key. What I expected to show up was a Message Box containing all of the coordinates. However, what showed up instead was just an empty message box.

Specific question

How can I update an Array object (in my case, the list_of_mouse_positions variable) by appending new rows to it, each one of which stores two new values?

Note about the expected dimensions of list_of_mouse_positions

I expect the list_of_mouse_positions variable to be a multidimensional array of dimensions (n x 2), where the first column contains all of the x values, and the second column contains all of the y values.

For example, suppose the user "captured" the coordinates of the following mouse positions:

  • (100, 200)
  • (400, 100)
  • (600, 150)

In this case, the structure of the list_of_mouse_positions would be as follows:

list_of_mouse_positions[1,1] = 100
list_of_mouse_positions[1,2] = 200
list_of_mouse_positions[2,1] = 400
list_of_mouse_positions[2,2] = 100
list_of_mouse_positions[3,1] = 600
list_of_mouse_positions[3,2] = 150

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧人哭 2025-02-19 19:56:54

更正的代码以匹配您的新代码,但使用超级全球编辑(请参见下面的DOC)

超级全球变量[v1.1.05+]:如果出现全局声明在任何函数之外,则默认情况下,它对所有函数都会生效(不包括力本地函数)。这避免了需要在每个函数中重新启动变量。但是,如果声明具有相同名称的函数参数或本地变量,则优先于全局变量。类关键字创建的变量也是超级全球。

关于您可能想做的简单性,或者您不想做的简单性,其中包括一些包含的评论:

;---------------;
; GENERAL SETUP ;
;---------------;
; Setting the coordinate mode
coordmode Mouse, screen

; This variable will store the mouse positions. 
; The command below establishes that it is a global variable. 
; Initializes the global variables
Global list_of_mouse_positions := Array()
Return ; end auto-execute section of script


;-----------;
; FUNCTIONS ;
;-----------;


; Adds the current mouse position to an array of mouse positions
AddLocation()
{
    ; global list_of_mouse_positions ; not needed, declared super-global above.
    MouseGetPos, xpos, ypos
    mouse_pos := Array() ; not needed see below
    mouse_pos.Push(xpos) ; not needed see below
    mouse_pos.Push(ypos) ; not needed see below
    list_of_mouse_positions.Push(mouse_pos)
    ; instead of making 3 additonal lines you can just do 'list_of_mouse_positions.Push(Array(xpos, ypos))'
    return
}

; Prints 2D arrays
print2DArr(byRef Arr, setDelim="`r`n", valDelim=", ") 
{
    printText := ""
    For _, Row in Arr 
    {
        printText .= (printText ? setDelim : "") . print1DArr(Row, valDelim)
    }
    Return printText
}

; Prints 1D arrays
print1DArr(byRef Array, valDelim=", ") 
{
    printText := ""
    For _, Val in Array 
    {
        printText .= (printText ? valDelim : "") . Val
    }
    Return printText
}

;-------------;
; KEYBINDINGS ;
;-------------;
; Adds new positions to the list of mouse positions
NumpadAdd::
    AddLocation()
    return

; Checking the mouse positions stored so far
NumPad0::
    global list_of_mouse_positions
    MsgBox % print2DArr(list_of_mouse_positions)
    return

Corrected code to match your new code, but with super-global edit (see doc quote below)

Super-global variables [v1.1.05+]: If a global declaration appears outside of any function, it takes effect for all functions by default (excluding force-local functions). This avoids the need to redeclare the variable in each function. However, if a function parameter or local variable with the same name is declared, it takes precedence over the global variable. Variables created by the class keyword are also super-global.

with some included comments on your code with regards to simplicity you may want to do, or not as you desire:

;---------------;
; GENERAL SETUP ;
;---------------;
; Setting the coordinate mode
coordmode Mouse, screen

; This variable will store the mouse positions. 
; The command below establishes that it is a global variable. 
; Initializes the global variables
Global list_of_mouse_positions := Array()
Return ; end auto-execute section of script


;-----------;
; FUNCTIONS ;
;-----------;


; Adds the current mouse position to an array of mouse positions
AddLocation()
{
    ; global list_of_mouse_positions ; not needed, declared super-global above.
    MouseGetPos, xpos, ypos
    mouse_pos := Array() ; not needed see below
    mouse_pos.Push(xpos) ; not needed see below
    mouse_pos.Push(ypos) ; not needed see below
    list_of_mouse_positions.Push(mouse_pos)
    ; instead of making 3 additonal lines you can just do 'list_of_mouse_positions.Push(Array(xpos, ypos))'
    return
}

; Prints 2D arrays
print2DArr(byRef Arr, setDelim="`r`n", valDelim=", ") 
{
    printText := ""
    For _, Row in Arr 
    {
        printText .= (printText ? setDelim : "") . print1DArr(Row, valDelim)
    }
    Return printText
}

; Prints 1D arrays
print1DArr(byRef Array, valDelim=", ") 
{
    printText := ""
    For _, Val in Array 
    {
        printText .= (printText ? valDelim : "") . Val
    }
    Return printText
}

;-------------;
; KEYBINDINGS ;
;-------------;
; Adds new positions to the list of mouse positions
NumpadAdd::
    AddLocation()
    return

; Checking the mouse positions stored so far
NumPad0::
    global list_of_mouse_positions
    MsgBox % print2DArr(list_of_mouse_positions)
    return
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文