-
-
Save ajinkyakulkarni/e1a1002279d938033a1be1b1d66443e2 to your computer and use it in GitHub Desktop.
File parsing, based on multiple lines, using recursive pattern matching with many :: (cons) operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
== MyItem 1 == | |
...some content... | |
Total: 10 | |
Success | |
== MyItem 2 == | |
...some content... | |
Total: 2 | |
Failed | |
== MyItem 3 == | |
...some content... | |
Total: 14 | |
Success | |
== MyItem 4 == | |
...some content... | |
Total: 7 | |
Failed |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if INTERACTIVE | |
;; | |
#else | |
module myParser | |
#endif | |
let file = @"c:\example.txt" | |
let lines = System.IO.File.ReadAllLines(file) | |
let countFailed = | |
let rec readLines (myLines:string list) resultItem resultdata = | |
match myLines with | |
| h::t when h.Contains("==") -> | |
let myItem = h.Substring(3, h.Length-6) | |
readLines t (myItem::resultItem) resultdata | |
| total::state::t when total.Contains("Total:") && state.StartsWith("Failed") -> | |
let info = (List.head resultItem), total.Substring(7) //, state ...etc information | |
readLines t resultItem (info::resultdata) | |
| h::t -> readLines t resultItem resultdata | |
| [] -> resultdata | |
readLines (lines |> Seq.toList) [] [] | |
// val countFailed : (string * string) list = [("MyItem 4", "7"); ("MyItem 2", "2")] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment