AutoHotKey - 使用变量的整数作为索引从数组中接收内容

AutoHotKey - Receive content from an array using a variables' integer as the index

我想从使用变量的整数作为数组索引的数组接收字符串。但它不起作用。

尝试 1

; Suspended | 0 = No, 1 = Yes
global Suspended     := 0
global SuspendedMsg  := ["The script has been paused.","The script has been re-activated."]

Pause::
    Suspend
    if suspended = 0 ; If script is not suspended
    {
        TrayTip, Paused, SuspendedMsg[Suspended], 3
        Suspended++
    } else ; If it is suspended
    {
        TrayTip, Activated, SuspendedMsg[Suspended], 3
        Suspended--
    }
return

尝试 #1 只会显示字符串 "SuspendedMsg[Suspended]",因为我不知道在哪里设置变量指示符 %。即使我将它设置为 SuspendedMsg[%Suspended%],它也会显示 [1] 或 [0]。

尝试 2

; Suspended | 0 = No, 1 = Yes
global Suspended      := 0
global SuspendedMsg   := ["The script has been paused.","The script has been re-activated."]
global SendSuspendMsg := SuspendedMsg[Suspended]

Pause::
    Suspend
    if suspended = 0 ; If script is not suspended
    {
        TrayTip, Paused, %SendSuspendMsg%, 3
        Suspended++
    } else ; If it is suspended
    {
        TrayTip, Activated, %SendSuspendMsg%, 3
        Suspended--
    }
return

尝试 #2 效果不佳,它甚至不显示任何消息。我尝试在 global SendSuspendMsg := SuspendedMsg[Suspended] 变量中用 % 摆弄 arround 但它不会做任何事情。有人愿意帮助我吗?

而不是TrayTip, Paused, SuspendedMsg[Suspended], 3TrayTip, Paused, SuspendedMsg[%Suspended%], 3,试试

TrayTip, Paused, % SuspendedMsg[Suspended], 3

。 TrayTip 要求您提供

specify the message to display

这相当于一个字符串。因此,变量名称在这里不作为变量处理,而是作为字符串处理(大多数时候在命令中)。声明 TrayTip, Paused, %SuspendedMsg[%Suspended%]%, 3 是有意义的 , 但不能嵌套变量的百分号。所以,我们必须使用百分号来强制表达式:

Force an expression: An expression can be used in a parameter that does not directly support it (except OutputVar parameters) by preceding the expression with a percent sign and a space or tab. In [v1.1.21+], this prefix can be used in the InputVar parameters of all commands except the traditional IF commands (use If (expression) instead). This technique is often used to access arrays.

关于你的第二个问题:我不认为数组可以那样声明,他们可以..? (但我不确定)。另见 this 短文。所以我猜问题出在你代码的第 3 行,因为其余部分对我来说看起来不错

@Blauhim 漏掉了一个重点,虽然他的回答大部分是正确的。首先像你一样创建数组中的索引,总是从 1 开始,然后继续到 2 等等......所以当你试图使用你的布尔变量来调用索引时你的代码是有缺陷的 0 索引确实不存在(更不用说你没有在那个 TrayTip 命令上强制和表达)。

; Set our variable to 1 why? Because we are going to use a Logical switch below.
Suspended     := 1
; This was correct format and I left it, although I removed Global's as they are not needed
SuspendedMsg  := ["The script has been paused.","The script has been re-activated."]

Pause::
    ; Suspend toggles each time it's called
    Suspend

    ; Here we are toggling the value of our variable using !
    ; We started with a 1 so that it would be correctly
    ;Changed to a 0 for the code below.
    suspended := !suspended

    ; Nothing changed here
    if suspended = 0 ; If script is not suspended
    {
        ; In order to pass an Array or Object or Expression to a Command you Force it
        ; using the a Percent Sign with a space on either side.
        ; Also note you were trying to use your Logical True/False 0 or 1 variable to         
        ; itterate. This didn't work because Array's always start with an Index of 1. 
        ; Below I've accounted for this by simply added a 1 to your suspended so it correctly 
        ; points to the Index in our Array.
        TrayTip, Paused, % SuspendedMsg[suspended + 1], 3

    } else ; If it is suspended
    {
        TrayTip, Activated, % SuspendedMsg[suspended + 1], 3        
    }
return