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

时间:2019-03-31 01:47:15

标签: csv parsing autohotkey strsplit

我想采用包含四个部分的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行。

我知道还有其他问题,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

删除行中多余的逗号

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

所以应该是

SplitRow := StrSplit(BaseRow, ",")