使用 autohotkey 将字符串转换为数字
String to Number using autohotkey
我想让我的 tail 函数获取日志文件中的最后一行并将其转换为数字。这样我就可以在 if 条件下使用它。
file = C:\Users\%A_UserName%\Documents\logTime.txt
Tail(k,file) ; Return the last k lines of file
{
Loop Read, %file%
{
i := Mod(A_Index,k)
L%i% = %A_LoopReadLine%
}
L := L%i%
Loop % k-1
{
IfLess i,1, SetEnv i,%k%
i-- ; Mod does not work here
L := L%i% "`n" L }
;Return L
;msgbox % Tail(1,file)
}
if 条件
While (PrLoad > 5 ) ; Assign the Number you want.
{
If (Tail(1, file) = %A_Hour%%A_Min%)
{
msgBox is equal to Current Time %Tail(1, file)%
Sleep 60000
}
Else if (Tail(1, file) > %A_Hour%%A_Min% )
{
msgBox Tail(1, file) is greater then %A_Hour%%A_Min%
Sleep 60000
}
日志文件由以下人员生成:
FileAppend, %A_Hour%%A_Min%`n, C:\Users\%A_UserName%\Documents\logTime.txt
我确信我将函数错误地传递给了 if 条件..%L%
如何将字符串转换为数字以供 if 语句进行比较?
您使用的是最新版本的 AutoHotkey 吗?如果没有,请从 autohotkey.com 或 ahkscript.org
下载最新版本
据我所知,您使用的是旧式伪数组。
在此处阅读 Objects/Arrays 的当前状态:
http://ahkscript.org/docs/Objects.htm
http://ahkscript.org/docs/objects/Object.htm
我看到的主要问题是在您的变量周围误用了 %。函数不需要 %% 的命令需要 %%.
我希望您知道 Tail(1, file) > %A_Hour%%A_Min%
可能会导致意想不到的结果。
假设 %A_Hour%%A_Min% 是 1250
和 Tail(1, file) returns 0105
.
01:05 可能发生在 12:50 之后,但您的脚本将看不到它。
现在您可以继续添加日、月和年,但这仍然不能消除所有问题。
这就是为什么大多数人使用时间戳,它只代表自 1970 年(或左右)以来已经过去了多少秒。
...
AHK 可以像处理数字一样处理字符串,所以应该没有任何问题。
试一试:
logFile = C:\Users\%A_UserName%\Documents\logTime.txt
;create a new timestamp and add it to the log
timestamp := GetUnixTimestamp()
FileAppend, %timestamp% `n, %logFile%
;wait a second
Sleep, 1000
;create another timestamp
currentTimestamp := GetUnixTimestamp()
;get old timestamp from log
timestampFromLog := FileGetLastLine(logFile)
MsgBox, %timestampFromLog% - Last timestamp from the log `n%currentTimestamp% - Current timestamp
If (currentTimestamp > timestampFromLog)
MsgBox, Everything ran as expected!
GetUnixTimestamp() {
T := A_NowUTC
T -= 1970,s
Return T
}
FileGetLastLine(file) {
Loop, Read, %file%
lineCount := A_Index
FileReadLine, lastLine, %file%, %lineCount%
Return lastLine
}
我想让我的 tail 函数获取日志文件中的最后一行并将其转换为数字。这样我就可以在 if 条件下使用它。
file = C:\Users\%A_UserName%\Documents\logTime.txt
Tail(k,file) ; Return the last k lines of file
{
Loop Read, %file%
{
i := Mod(A_Index,k)
L%i% = %A_LoopReadLine%
}
L := L%i%
Loop % k-1
{
IfLess i,1, SetEnv i,%k%
i-- ; Mod does not work here
L := L%i% "`n" L }
;Return L
;msgbox % Tail(1,file)
}
if 条件
While (PrLoad > 5 ) ; Assign the Number you want.
{
If (Tail(1, file) = %A_Hour%%A_Min%)
{
msgBox is equal to Current Time %Tail(1, file)%
Sleep 60000
}
Else if (Tail(1, file) > %A_Hour%%A_Min% )
{
msgBox Tail(1, file) is greater then %A_Hour%%A_Min%
Sleep 60000
}
日志文件由以下人员生成:
FileAppend, %A_Hour%%A_Min%`n, C:\Users\%A_UserName%\Documents\logTime.txt
我确信我将函数错误地传递给了 if 条件..%L%
如何将字符串转换为数字以供 if 语句进行比较?
您使用的是最新版本的 AutoHotkey 吗?如果没有,请从 autohotkey.com 或 ahkscript.org
下载最新版本据我所知,您使用的是旧式伪数组。
在此处阅读 Objects/Arrays 的当前状态:
http://ahkscript.org/docs/Objects.htm http://ahkscript.org/docs/objects/Object.htm
我看到的主要问题是在您的变量周围误用了 %。函数不需要 %% 的命令需要 %%.
我希望您知道 Tail(1, file) > %A_Hour%%A_Min%
可能会导致意想不到的结果。
假设 %A_Hour%%A_Min% 是 1250
和 Tail(1, file) returns 0105
.
01:05 可能发生在 12:50 之后,但您的脚本将看不到它。
现在您可以继续添加日、月和年,但这仍然不能消除所有问题。
这就是为什么大多数人使用时间戳,它只代表自 1970 年(或左右)以来已经过去了多少秒。
...
AHK 可以像处理数字一样处理字符串,所以应该没有任何问题。
试一试:
logFile = C:\Users\%A_UserName%\Documents\logTime.txt
;create a new timestamp and add it to the log
timestamp := GetUnixTimestamp()
FileAppend, %timestamp% `n, %logFile%
;wait a second
Sleep, 1000
;create another timestamp
currentTimestamp := GetUnixTimestamp()
;get old timestamp from log
timestampFromLog := FileGetLastLine(logFile)
MsgBox, %timestampFromLog% - Last timestamp from the log `n%currentTimestamp% - Current timestamp
If (currentTimestamp > timestampFromLog)
MsgBox, Everything ran as expected!
GetUnixTimestamp() {
T := A_NowUTC
T -= 1970,s
Return T
}
FileGetLastLine(file) {
Loop, Read, %file%
lineCount := A_Index
FileReadLine, lastLine, %file%, %lineCount%
Return lastLine
}