使用自动快捷键将字符串转换为数字 [英] String to Number using autohotkey

查看:562
本文介绍了使用自动快捷键将字符串转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想让我的tail函数获取日志文件中的最后一行并将其转换为数字.这样我就可以在if条件下使用它.

I would like to get my tail function to grab the last line in logfile and turn it into a number. So that I can then use it in a if condition.

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
}

日志文件是由以下人员创建的:

Logfile is being made by the following:

FileAppend, %A_Hour%%A_Min%`n, C:\Users\%A_UserName%\Documents\logTime.txt

我确定我会将函数错误传递给if条件..%L% 如何将字符串转换为要与if语句进行比较的数字?

I am as sure im passing the function wrong into the if condition..%L% how can I turn the string into a number to be compared by the if statments?

推荐答案

我希望您知道Tail(1, file) > %A_Hour%%A_Min%可能导致意外结果的事实.

I hope you are aware of the fact that Tail(1, file) > %A_Hour%%A_Min% may lead to unexpected results.

假设%A_Hour %% A_Min%是1250,并且Tail(1,file)返回0105.
01:05可能会在12:50之后发生,但是您的脚本将看不到它.
现在您可以继续添加日期,月份和年份,但这仍不能消除所有问题.

Let's say %A_Hour%%A_Min% is 1250 and Tail(1, file) returns 0105.
01:05 may take place after 12:50, but your script would fail to see that.
Now you could go on and add the day, month and year to it, but that would still not eliminate all issues.

这就是为什么大多数人使用时间戳的原因,该时间戳仅表示自1970年以来(大约)过去了多少秒.

That's why most people use timestamps which just respresent how many seconds have passed by since 1970 (or so).

... AHK可以像对待数字一样处理字符串,因此完全没有问题.
试试看:

... AHK can work with strings as if they are numbers, so there shouldn't be any problems at all with that.
Give this a try:

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
}

这篇关于使用自动快捷键将字符串转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆