【发布时间】:2016-03-04 06:17:08
【问题描述】:
我正在编写一个程序,它比较远程文件的内容并实时记录(而不是下载文件并每次用它的最新“版本”替换它)它的内容到本地文件。为此,我创建了一个 FTP 连接,读取远程文件,跳过所有内容直到读取的最后一行,然后将所有新行复制到本地文件中。
为了比较新与否,我使用了以下函数:
let getNewLines (lineToLookFor:string) (varLogLines:seq<string>) =
let newLines = Seq.skipWhile ((fun (lineToMatch:string) (varLogLine:string) -> not(lineToMatch.Equals(varLogLine))) lineToLookFor) varLogLines
match Seq.length newLines with
| 0 -> (varLogLines, lineToLookFor) // There has been a rollover of the messages file, take all the lines as they are all new
| _ -> (newLines, Seq.last newLines) // Only take the new lines which were written since the last check in the messages file
Seq.skipWhile 应该跳过 seq 中谓词函数返回 true 的所有条目,然后返回 seq 的其余部分。就我而言,我想读取所有行,直到 lineToMatch.Equals(varLogLine),然后在 seq 中返回该点之后的所有其他内容。
不幸的是,newLines 总是空的,因此我的程序的流程总是在第一个匹配中进行 | 0->。我不知道为什么。
为了记录,我也尝试过 Contains 而不是 Equals,但没有任何成功。
【问题讨论】:
-
您的代码看起来不错(例如,
getNewLines "x" [for c in 'a' .. 'z' -> string c]的结果符合预期)。也许您的lineToLookFor以回车或其他内容结尾?