Skip to content

Instantly share code, notes, and snippets.

@Thorium
Last active December 13, 2017 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Thorium/6088882 to your computer and use it in GitHub Desktop.
Save Thorium/6088882 to your computer and use it in GitHub Desktop.
File parsing, based on multiple lines, using recursive pattern matching with many :: (cons) operator
== 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
#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