Extract the last pair of matching entries for time calculations

Ask gaming related questions (AHK v1.1 and older)
GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Extract the last pair of matching entries for time calculations

Post by GoneFishing » 19 Mar 2023, 11:01

This script retrieves entries to calculate the session time for each pair of values in a log file.

Code: Select all

#Requires AutoHotkey v1.1.33
inputFile := A_ScriptDir "\TestClient.txt"
Loop Read, % inputFile
{ timeStamp := SubStr(A_LoopReadLine, 1, 19)
  If InStr(A_LoopReadLine, "*")               ; Log file opening
   (t1) && showDif(t1, t2), t1 := RegExReplace(timeStamp, "[/: ]")
  Else t2 := RegExReplace(timeStamp, "[/: ]") ; Not log file opening
}
showDif(t1, t2)

showDif(t1, t2) {
 Static n := 0
 dif := t2
 dif -= t1, S
 MsgBox 0, % "Result #" ++n, % Format("t1 = {}`nt2 = {}`n`nDifference = {:.1f} min", t1, t2, dif / 60)
}
I need to grab the last pair of entries and use the total time difference between the two (in seconds) to determine a follow up action. The calculation to get seconds is easy enough (just remove the 60) and I can perform the subsequent if statement with the value in a variable but how do I capture just the last pair of values to use ? Test script is attached below (it's the 4th result - 1945 seconds I need to extract).
Attachments
TestClient.txt
(368.8 KiB) Downloaded 13 times

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Extract the last pair of matching entries for time calculations

Post by mikeyww » 20 Mar 2023, 08:26

This is a continuation of a previous thread.

viewtopic.php?f=18&t=115076

Code: Select all

#Requires AutoHotkey v1.1.33
inputFile := A_ScriptDir "\TestClient.txt"
Loop Read, % inputFile
{ timeStamp := SubStr(A_LoopReadLine, 1, 19)
  If InStr(A_LoopReadLine, "*")               ; Log file opening
   (t1) && showDif(t1, t2), t1 := RegExReplace(timeStamp, "[/: ]")
  Else t2 := RegExReplace(timeStamp, "[/: ]") ; Not log file opening
}
MsgBox % lastDif := showDif(t1, t2)

showDif(t1, t2) {
 Static n := 0
 dif := t2
 dif -= t1, S
 ; MsgBox 0, % "Result #" ++n, % Format("t1 = {}`nt2 = {}`n`nDifference = {:.1f} min", t1, t2, dif / 60)
 Return dif
}

GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

Re: Extract the last pair of matching entries for time calculations

Post by GoneFishing » 20 Mar 2023, 10:48

It is a continuation but I thought it best to make a new post. Thanks once again for providing a solution !

User avatar
mikeyww
Posts: 26891
Joined: 09 Sep 2014, 18:38

Re: Extract the last pair of matching entries for time calculations

Post by mikeyww » 20 Mar 2023, 11:03

An alternative is below.

Code: Select all

#Requires AutoHotkey v1.1.33
inputFile := A_ScriptDir "\TestClient.txt"
timestamp := "(\d{4}/\d\d/\d\d \d\d:\d\d:\d\d)"
regex     := "s).+" timestamp " \*.+" timestamp
FileRead txt, % inputFile
RegExMatch(txt, regex, t)
MsgBox % lastDif := showDif(cleanTS(t1), cleanTS(t2))

showDif(t1, t2) {
 Static n := 0
 dif := t2
 dif -= t1, S
 Return dif
}

cleanTS(timestamp) {
 Return RegExReplace(timestamp, "[/: ]")
}

Post Reply

Return to “Gaming Help (v1)”