在 AHK 中,如何正确使用 StrSplit 从 CSV 文件创建数组,然后根据特定条件对其进行解析?



我想采用一个包含四个部分的CSV: 要显示的一些文本、优先级、持续时间(以毫秒为单位)和"开始时间"(以毫秒为单位) 然后从行中选择开始时间最短的行(或者在平局的情况下,选择优先级最高的行)。

每当我尝试运行当前代码时,它都会返回一个错误,指出我的 StrSplit 行有一个空白参数。

我尝试做一个单独的读取循环来填充数组,然后使用内置的 Index 变量来跟踪每个变量。我怀疑问题在于数组通常只是一组用逗号分隔的数据片段——并且其中有一行包含逗号可能会导致问题——所以我的循环可能运行一次,然后在第二次尝试时返回空白拆分。

; Create empty array to be read to
StringArray := []
;~ ; Loop through CSV file and append each line to the array
Loop, Read, Gamestrings.csv
{
StringArray.Push(A_LoopReadLine)
}
return
; New Table Option
;~ StringArray := Object()
;~ Loop, Read, Gamestrings.csv
;~ StringArray[A_Index] := StrSplit(A_LoopReadLine, ",")
;~ return
;Set up function/label
; Outerloop checks for Indices in array only if there is no active string, if there is an active string, loop portion skipped in favor of checking SplitStartTime against global counter until a string is displayed, which sets activestring to empty
StartCheck:
{
for index, element in StringArray {
BaseRow := StringArray[A_Index]
SplitRow := StrSplit(BaseRow, ",",)
SplitStartTime := SplitRow[4]
SplitPriority := SplitRow[2]
}
if (CurrentLowest == false OR SplitStartTime < CurrentLowest) {
;Create initial Active String for comparisons
ActiveString := StringArray[A_index]
HighestPriority := SplitPriority
CurrentLowest := SplitStartTime
ActiveIndex := A_index
}
;~ else if (SplitStartTime < CurrentLowest) {
;Replace Global Variables with current line OR could use StringArray index to point to which line should be active and save on using so many variables
;~ ActiveString := StringArray[A_index]
;~ DisplayText := SplitRow[1]
;~ PriorityOne := SplitRow[2]
;~ DisplayDuration := SplitRow[3]
;~ HighestPriority := SplitPriority
;~ CurrentLowest := SplitStartTime
;~ }
else if (SplitStartTime = CurrentLowest AND HighestPriority < SplitPriority) {
ActiveString := StringArray[A_index]
HighestPriority := SplitPriority
ActiveIndex := A_index

整个脚本的最终目标是在特定持续时间内显示一行文本,然后将其从数组中删除。数组是从 CSV 文件填充的。一次只能显示一行,所以我有一个伪计时器纵为全局变量,但甚至无法测试脚本的功能,因为我从 StrSplit 行得到空白参数错误。

我知道还有其他问题——任何帮助都是值得赞赏的。

删除行中多余的逗号

SplitRow := StrSplit(BaseRow, ",",)

所以,它应该是

SplitRow := StrSplit(BaseRow, ",")

相关内容

最新更新